O'Reilly logo

From Java to C#: A Developer's Guide by Heng Ngee Mok

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required