Exception Handling
Explain exception handling in c sharp.
Exception handling in C#, supported by the try catch and finaly block is a mechanism to detect and handle run-time errors in code. The .NET framework provides built-in classes for common exceptions. The exceptions are anomalies that occur during the execution of a program. They can be because of user, logic or system errors. If a user (programmer) does not provide a mechanism to handle these anomalies, the .NET runtime environment provide a default mechanism, which terminates the program execution.
try..catch..finally
C# provides three keywords try, catch and finally to implement exception handling. The try encloses the statements that might throw an exception whereas catch handles an exception if one exists. The finally can be used for any cleanup work that needs to be done.
If any exception occurs inside the try block, the control transfers to the appropriate catch block and later to the finally block.
But in C#, both catch and finally blocks are optional. The try block can exist either with one or more catch blocks or a finally block or with both catch and finally blocks.
If there is no exception occurred inside the try block, the control directly transfers to finally block. We can say that the statements inside the finally block is executed always. Note that it is an error to transfer control out of a finally block by using break, continue, return or goto.
In C#, exceptions are nothing but objects of the type Exception. The Exception is the ultimate base class for any exceptions in C#. The C# itself provides couple of standard exceptions. Or even the user can create their own exception classes, provided that this should inherit from either Exception class or one of the standard derived classes of Exception class like DivideByZeroExcpetion to ArgumentException etc.
Uncaught Exceptions
The following program will compile but will show an error during execution. The division by zero is a runtime anomaly and program terminates with an error message. Any uncaught exceptions in the current context propagate to a higher context and looks for an appropriate catch block to handle it. If it can't find any suitable catch blocks, the default mechanism of the .NET runtime will terminate the execution of the entire program.
- //C#: Exception Handling
- //Author: rajeshvs@msn.com
- using System;
- class MyClient
- {
- public static void Main()
- {
- int x = 0;
- int div = 100/x;
- Console.WriteLine(div);
- }
- }
The modified form of the above program with exception handling mechanism is as follows. Here we are using the object of the standard exception class DivideByZeroException to handle the exception caused by division by zero.
- //C#: Exception Handling
- using System;
- class MyClient
- {
- public static void Main()
- {
- int x = 0;
- int div = 0;
- try
- {
- div = 100 / x;
- Console.WriteLine("This linein not executed");
- }
- catch (DivideByZeroException)
- {
- Console.WriteLine("Exception occured");
- }
- Console.WriteLine($"Result is {div}");
- }
- }
In the above case, the program do not terminate unexpectedly. Instead, the program control passes from the point where exception occurred inside the try block to the catch blocks. If it finds any suitable catch block, executes the statements inside that catch and continues with the normal execution of the program statements.
If a finally block is present, the code inside the finally block will get also be executed.
- //C#: Exception Handling
- using System;
- class MyClient
- {
- public static void Main()
- {
- int x = 0;
- int div = 0;
- try
- {
- div = 100/x;
- Console.WriteLine("Not executed line");
- }
- catch(DivideByZeroException)
- {
- Console.WriteLine("Exception occured");
- }
- finally
- {
- Console.WriteLine("Finally Block");
- }
- Console.WriteLine($"Result is {div}");
- }
- }