Exception Handling - CSharp Custom Type

CSharp examples for Custom Type:Exception

Introduction

The syntax for using try/catch looks like the following

try {
   // statements causing exception
} catch( ExceptionName e1 ) {
   // error handling code
} catch( ExceptionName e2 ) {
   // error handling code
} catch( ExceptionName eN ) {
   // error handling code
} finally {
   // statements to be executed
}

Demo Code

using System;/*from  w w w. ja v  a  2 s. co m*/
class DivNumbers {
   int result;
   DivNumbers() {
      result = 0;
   }
   public void division(int num1, int num2) {
      try {
         result = num1 / num2;
      } catch (DivideByZeroException e) {
         Console.WriteLine("Exception caught: {0}", e);
      } finally {
         Console.WriteLine("Result: {0}", result);
      }
   }
   static void Main(string[] args) {
      DivNumbers d = new DivNumbers();
      d.division(25, 0);
   }
}

Result


Related Tutorials