What is Exception?
The exceptions are anomalies that occur during the execution of a program. Exception handling is a mechanism in .NET framework to detect
and handle run time errors. They can be because of user, logic or system errors. If a user (programmer) do not provide a mechanism to handle these anomalies, the .NET run time environment provide a default mechanism, which terminates the program execution.
In C# there are three keywords Try, Catch and Finally for handling exceptions.
In try block statements it might throw an exception whereas catch handles that caused by try block if one exists.
The finally block is used for doing any clean up process. The statement in finally block always executes.
e.g.-
and handle run time errors. They can be because of user, logic or system errors. If a user (programmer) do not provide a mechanism to handle these anomalies, the .NET run time environment provide a default mechanism, which terminates the program execution.
In C# there are three keywords Try, Catch and Finally for handling exceptions.
In try block statements it might throw an exception whereas catch handles that caused by try block if one exists.
The finally block is used for doing any clean up process. The statement in finally block always executes.
e.g.-
try { // this can cause an exception. } catch (Type x) { // for handling the exception. } finally { //this will execute. } |
Handling Exceptions
In catch block, if don't use a brackets or arguments, we can catch all exceptions occurred inside a try block. Even we can use a catch block with an Exception type parameter to catch all exceptions happened inside the try block.
Since in C#, all exceptions are directly or indirectly inherited from the Exception class.
e.g.
class newexp { public static void Main() { int a = 0; int div = 0; try { div = 100/a; Console.WriteLine("This will not print"); } catch { Console.WriteLine("oException" ); } Console.WriteLine("Result is {0}",div); } } |
Exceptions Classes Following are some common exception classes. SystemException- This exception means a failed run-time check used as a base class for other. AccessException- This exception means failure to access a type member, such as a method or field. ArgumentException- This exception means an argument to a method was invalid. ArgumentNullException- This exception means a null argument was passed to a method that doesn't accept it. ArgumentOutOfRangeException-This exception means argument value is out of range. ArithmeticException- This exception means arithmetic over – or underflow has occurred. ArrayTypeMismatchException- This exception means attempt to store the wrong type of object in an array. BadImageFormatException- This exception means image is in the wrong format. CoreException- This exception means base class for exceptions thrown by the runtime. DivideByZeroException- This exception means an attempt was made to divide by zero. FormatException- This exception means the format of an argument is wrong. IndexOutOfRangeException- This exception means an array index is out of bounds. InvalidCastExpression- This exception means an attempt was made to cast to an invalid class. InvalidOperationException- This exception means a method was called at an invalid time. MissingMemberException- This exception means an invalid version of a DLL was accessed. NotFiniteNumberException- This exception means a number is not valid. NotSupportedException- This exception means indicates that a method is not implemented by a class. NullReferenceException- This exception means attempt to use an unassigned reference. OutOfMemoryException- This exception means not enough memory to continue execution. StackOverflowException- This exception means a stack has overflow.
No comments:
Post a Comment