C# Exception Handling :

What Is an Exception?

When we write a program, we describe step-by-step what the computer must do (at least in imperative programming; in the functional programming things look a bit different) and in most of the cases we rely that the program will execute normally. Indeed, most of the time, programs are following this normal pattern, but there are some exceptions. Let’s say we want to read a file and display its contents on the screen. Let’s assume the file is located on a remote server and during the process of reading it, the connection goes down. The file then will be only partially loaded. The program will not be able to execute normally and show file’s contents on the screen. In this case, we have an exception from the normal (and correct) program execution and this exception must be reported to the user and/or the administrator.

Catching and Handling Exceptions :

Exception handling is a mechanism, which allows exceptions to be thrown and caught. This mechanism is provided internally by the CLR (Common Language Runtime). Parts of the exception handling infrastructure are the language constructs in C# for throwing and catching exceptions. CLR takes care to propagate each exception to the code that can handle it.

Example 1 :

  1. /* Description: throw an exception example */
  2. using System;
  3. using System.IO;
  4. namespace DemoProgramming
  5. {
  6. class Program
  7. {
  8. static void Main()
  9. {
  10. string fileName = "myTextFile.txt";
  11. ReadFile(fileName);
  12. Console.ReadKey();
  13. }
  14. static void ReadFile(string fileName)
  15. {
  16. TextReader reader = new StreamReader(fileName);
  17. string line = reader.ReadLine();
  18. Console.WriteLine(line);
  19. reader.Close();
  20. }
  21. }
  22. }

Output 1 :

C Sharp Language throw an exception example

Note : "myTextFile.txt" file store "YourProjectLocation(ConsoleApplication) ⇁ bin ⇁ Debug ⇁ myTextFile.txt "


Example 2 :

  1. /* Description: another throw an exception example */
  2. using System;
  3. using System.IO;
  4. namespace DemoProgramming
  5. {
  6. class Program
  7. {
  8. static void Main()
  9. {
  10. try
  11. {
  12. string fileName = "myTextFile.txt";
  13. ReadFile(fileName);
  14. }
  15. catch (Exception e)
  16. {
  17. throw new ApplicationException("Bad thing happened", e);
  18. }
  19. Console.ReadKey();
  20. }
  21. static void ReadFile(string fileName)
  22. {
  23. try
  24. {
  25. TextReader reader = new StreamReader(fileName);
  26. string line = reader.ReadLine();
  27. Console.WriteLine(line);
  28. reader.Close();
  29. }
  30. catch (FileNotFoundException)
  31. {
  32. catch (FileNotFoundException)
  33. Console.WriteLine("The file {0} does not exist!", fileName);
  34. }
  35. }
  36. }
  37. }

Output 2 :

C Sharp Language another throw an exception example

Note : Here we change "myTextFile.txt" strings so, here output is deferent then "Example 1"


Next Update

Loading...

Friend's some reason we need some long time for next update, Sorry For Long Time, see you soon.

We make a Collaboration with HackIsOn YouTube Channel So, Please Subscribe Their Channel and keep supporting