13.1. Exception examples
Three examples are given here as a refresher:
basic try-catch-finally;
nested try;
user-defined exception.
Read through these examples to familiarize yourself with the different types of exception objects thrown for each scenario. Note the comments I have made at the end of each example.
13.1.1. Exception example 1: basic try-catch-finally
Here is a basic example of exception handling in C#. Try to trace the program flow based on your knowledge of Java's exception handling.
1: using System; 2: 3: class TestClass{ 4: public static void Main(){ 5: 6: Console.Write("Enter a number :"); 7: string userInput = Console.ReadLine(); 8: 9: try{ 10: int number1 = Convert.ToInt32(userInput); 11: int number2 = 1/number1; 12: } 13: ...
Get From Java to C#: A Developer's Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.