Error Types

Exception

All exceptions are predefined exceptions.

Categorized of exception

Example

An System.IO.IOException exception is thrown when you try to access an illegal stream object.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace Exception2
  5. {
  6. class Test
  7. {
  8. static int Zero = 0;
  9. static void AFunction()
  10. {
  11. try
  12. {
  13. int j = 22 / Zero;
  14. }
  15. // this exception doesn't match
  16. catch (DivideByZeroException e)
  17. {
  18. Console.WriteLine(e.Message);
  19. }
  20. Console.WriteLine("In AFunction()");
  21. }
  22. //class Program
  23. //{
  24. public static void Main()
  25. {
  26. try
  27. {
  28. AFunction();
  29. }
  30. // this exception doesn't match
  31. catch (ArgumentException e)
  32. {
  33. Console.WriteLine("ArgumentException {0}", e);
  34. }
  35. Console.ReadKey();
  36. }
  37. //}
  38. }
  39. }

See the following output.



So friends, these are a few comments that will help you with samples of exceptions in C#. The next time I will put some more interesting comments, thank you. I hope this is helpful for you. Enjoy :).