The Exception Handling in C# is a process to handle runtime errors. You should perform exception handling so that normal flow of the application can be maintained even after runtime errors. The exception is a problem that arises during the execution of a program. The C# exception is a response to an exceptional situation that arises while a program is running, such as an attempt to divide by zero. An Exceptions provide a way to transfer control from one part of a program to another. The C# exception handling is built upon four keywords: try, catch, finally, and throw.
Try − The try block identifies a block of code for which particular exceptions is activated. This is followed by one or more catch blocks.
Catch – A C# program catches an exception with an exception handler at the place in a program where you want to handle the problem. That is the catch keyword indicates the catching of an exception.
Finally − A finally block is used to execute a given set of statements, whether an exception is thrown or not thrown. By an example, if you open a file, it must be closed whether an exception is raised or not.
Throw – A C# program throws an exception when a problem shows up. This is done using a throw keyword. We can throw an object if it is either directly or indirectly derived from the System. Exception class. We can use a throw statement in the catch block to throw the present object as –
Catch (Exception e) {
...
Throw e
}
//C#: Exception Handling:
using System; class Customer { public static void Main() { try { throw new DivideByZeroException("Invalid Division"); }
catch (DivideByZeroException) { Console.WriteLine("Exception"); }
Console.WriteLine("LAST STATEMENT"); } }
Note: - A try block may have multiple catch block.
The Exception handling is an inbuilt mechanism in the .NET framework to detect and handle run-time errors. The .NET framework contains a lot of standard exceptions. And the exceptions are anomalies that occur during the execution of a program. That can be because of the user, logic or system errors. If the programmer (user) does not provide a mechanism to handle these anomalies, the .NET runtime environment provides a default mechanism, which terminates the program execution.
Example - 1
using System; using System.IO;
class FinallyDemo { static void Main(string[] args) { FileStream outStream = null; FileStream inStream = null;
try { outStream = File.OpenWrite("DestinationFile.txt"); inStream = File.OpenRead("BogusInputFile.txt"); } catch(Exception ex) { Console.WriteLine(ex.ToString()); }
finally { if (outStream != null) { outStream.Close(); Console.WriteLine("outStream closed."); } if (inStream != null) { inStream.Close(); Console.WriteLine("inStream closed."); } } } }
Example - 2
using System; namespace ExceptionHandlingApplication {
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); Console.ReadKey(); } } }
Anonymous User
13-Dec-2018Exception Handling in C#
The Exception Handling in C# is a process to handle runtime errors. You should perform exception handling so that normal flow of the application can be maintained even after runtime errors. The exception is a problem that arises during the execution of a program. The C# exception is a response to an exceptional situation that arises while a program is running, such as an attempt to divide by zero. An Exceptions provide a way to transfer control from one part of a program to another. The C# exception handling is built upon four keywords: try, catch, finally, and throw.
Try − The try block identifies a block of code for which particular exceptions is activated. This is followed by one or more catch blocks.
Catch – A C# program catches an exception with an exception handler at the place in a program where you want to handle the problem. That is the catch keyword indicates the catching of an exception.
Finally − A finally block is used to execute a given set of statements, whether an exception is thrown or not thrown. By an example, if you open a file, it must be closed whether an exception is raised or not.
Throw – A C# program throws an exception when a problem shows up. This is done using a throw keyword. We can throw an object if it is either directly or indirectly derived from the System. Exception class. We can use a throw statement in the catch block to throw the present object as –
Note: - A try block may have multiple catch block.
The Exception handling is an inbuilt mechanism in the .NET framework to detect and handle run-time errors. The .NET framework contains a lot of standard exceptions. And the exceptions are anomalies that occur during the execution of a program. That can be because of the user, logic or system errors. If the programmer (user) does not provide a mechanism to handle these anomalies, the .NET runtime environment provides a default mechanism, which terminates the program execution.
Namespace in C# for ExceptionHandling :-
1. System.OutOfMemoryException
2. System.NullReferenceException
3. Syste.InvalidCastException
4. Syste.ArrayTypeMismatchException
5. System.IndexOutOfRangeException
6. System.ArithmeticException
7. System.DevideByZeroException
8. System.OverFlowException
Thank You for Reading this article...and If any suggestion for me, please comment me by the comment box.