An exception is a problem that occurs during the execution of a program. The C # exception is an answer to extraordinary circumstances which is running a program, such as attempting to divide by zero.
Exceptions provide a way to transfer control from one part of a program to another. The C # exception handling is made on four keywords: try, catch, finally and throw
Try - a try block identifies a block of code for which special exception is active. After this, one or more cash blocks
catch- A program spreads exception with an exception handler in such a program, where you want to handle the problem. The catch keyword prompts an exception to catch.
Finally - the block is used to execute a set of statements, even if the exception is not thrown or thrown. For example, if you open a file, it should be closed regardless of whether an exception has been raised or not.
Throw - One problem throws an exception when a problem appears that is done using the throw keyword
Exceptions provide a way to transfer control from one part of a program to another. The C # exception handling is made on four keywords: try, catch, finally and throw
Try - a try block identifies a block of code for which special exception is active. After this, one or more cash blocks
catch- A program spreads exception with an exception handler in such a program, where you want to handle the problem. The catch keyword prompts an exception to catch.
Finally - the block is used to execute a set of statements, even if the exception is not thrown or thrown. For example, if you open a file, it should be closed regardless of whether an exception has been raised or not.
Throw - One problem throws an exception when a problem appears that is done using the throw keyword
Syntax
try { // statements causing exception } catch( ExceptionName e1 ) { // error handling code } catch( ExceptionName e2 ) { // error handling code } catch( ExceptionName eN ) { // error handling code } finally { // statements to be executed }
You can list down multiple catch statements to catch different type of exceptions in case your try block raises more than one exception in different situations.
Exception Classes in C#
Sr.No. | Exception Class & Description |
---|---|
1 |
System.IO.IOException
Handles I/O errors.
|
2 |
System.IndexOutOfRangeException
Handles errors generated when a method refers to an array index out of range.
|
3 |
System.ArrayTypeMismatchException
Handles errors generated when type is mismatched with the array type.
|
4 |
System.NullReferenceException
Handles errors generated from referencing a null object.
|
5 |
System.DivideByZeroException
Handles errors generated from dividing a dividend with zero.
|
6 |
System.InvalidCastException
Handles errors generated during typecasting.
|
7 |
System.OutOfMemoryException
Handles errors generated from insufficient free memory.
|
8 |
System.StackOverflowException
Handles errors generated from stack overflow.
|
Handling Exceptions
C # provides a structured solution to the exception handling in the form of try and catch blocks. Using these programs
using System; namespace ErrorHandlingApplication { 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(); } } }
0 comments:
Post a Comment