Library tutorials & articles
Exception Handling In C#
- Introduction
- Try... Catch
- Exception Types
- Throwing an error
Exception Types
There are a number of exception classes provided by C#, all of which inherit from the System.Exception class. Following are some common exception classes.
| Exception Class | Cause |
SystemException
|
A failed run-time check;used as a base class for other. |
AccessException
|
Failure to access a type member, such as a method or field. |
ArgumentException
|
An argument to a method was invalid. |
ArgumentNullException
|
A null argument was passed to a method that doesn't accept it. |
ArgumentOutOfRangeException
|
Argument value is out of range. |
ArithmeticException
|
Arithmetic over - or underflow has occurred. |
ArrayTypeMismatchException
|
Attempt to store the wrong type of object in an array. |
BadImageFormatException
|
Image is in the wrong format. |
CoreException
|
Base class for exceptions thrown by the runtime. |
DivideByZeroException
|
An attempt was made to divide by zero. |
FormatException
|
The format of an argument is wrong. |
IndexOutOfRangeException
|
An array index is out of bounds. |
InvalidCastExpression
|
An attempt was made to cast to an invalid class. |
InvalidOperationException
|
A method was called at an invalid time. |
MissingMemberException
|
An invalid version of a DLL was accessed. |
NotFiniteNumberException
|
A number is not valid. |
NotSupportedException
|
Indicates sthat a method is not implemented by a class. |
NullReferenceException
|
Attempt to use an unassigned reference. |
OutOfMemoryException
|
Not enough memory to continue execution. |
StackOverflowException
|
A stack has overflown. |
The finally block is used to do all the clean up code. It does not support the error message, but all the code contained in the finally block is executed after the exception is raised. We can use this block along with try-catch and only with catch too.
The finally block is executed even if the error is raised. Control is always passed to the finally block regardless of how the try blocks exits.
This is shown in the following example:
int a, b = 0 ;
Console.WriteLine( "My program starts" ) ;
try
{
a = 10 / b;
}
catch ( InvalidOperationException e )
{
Console.WriteLine ( e ) ;
}
catch ( DivideByZeroException e)
{
Console.WriteLine ( e ) ;
}
finally
{
Console.WriteLine ( "finally" ) ;
}
Console.WriteLine ( "Remaining program" ) ;
The output here is:
My program starts
System.DivideByZeroException: Attempted to divide by zero.
at ConsoleApplication4.Class1.Main(String[] args) in d:\programs\consoleapplication4\class1.cs:line
51
finally
Remaining program
But then what's the difference? We could have written
Console.WriteLine ("finally");
after the catch block, and not write the finally block at all. Writing finally did not make much of a difference. Anyway the code written after catch gets executed. The answer to this is not clear in this program. It will be clear when we see the try-finally and the throw statement.
Related articles
Related discussion
-
how to select multiple files at a time using Ctrl+select and upload the attachments in C# .Net 1.0?
by vasanta (0 replies)
-
Help please for student
by mandy130 (0 replies)
-
Loop help needed
by BKRoberts (5 replies)
-
Problem after strong naming an assembly
by rinkurathor1 (0 replies)
-
Regarding User control creation in C# .Net(Windows Application)
by porchelvi (0 replies)
Related podcasts
-
Looking into the C# Crystal Ball with Charlie Calvert and Bill Wagner
One of the most exciting announcements from PDC was the news about C# 4.0 and Visual Studio 2010. With all the excitement and discussion throughout the event about these new developer tools, we reached out to two experts in the fields. Charlie Calvert and Bill Wagner sat down with Keith and Woody...
Events coming up
-
Dec
6
Developing AJAX Web Applications with Castle Monorail
London, United Kingdom
Monorail is the model-view-controller engine of the Castle Project, bringing many of the best ideas of Ruby on Rails to the .NET world. In this talk, David De Florinier and Gojko Adzic show how Monorail makes it easy to develop .NET based AJAX applications, and how to use the Castle Project to build Web 2.0 applications effectively. Come to this session if you are a .NET web developer. Everyone is welcome!
This article has errors at the please don't publish articles with errors.
Thanks
Sri
reinstall both os and software..and add urself in degbugger group
vxy
hi reinstall windows and remove and install .net and add urself in debugger gruops.
This article has errors at the please don't publish articles with errors.
Thanks
Sri
This thread is for discussions of Exception Handling In C#.