Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Catching EXCEPTIONS in C#

Using TRY and CATCH blocks, you can catch the exceptions.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace ForgetCode
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. int top = 0, bottom = 0, result = 0;
  13. try
  14. {
  15. result = top / bottom;
  16. }
  17. catch (System.DivideByZeroException ex)
  18. {
  19. Console.WriteLine("{0} Exception caught.", ex);
  20. }
  21. }
  22. }
  23. }


Output:
  1. System.DivideByZeroException: Attempted to divide by zero.