Library tutorials & articles
Exception Handling In C#
- Introduction
- Try... Catch
- Exception Types
- Throwing an error
Throwing an error
Consider the following code snippet
int a, b = 0 ;
Console.WriteLine( "My program starts" )
try
{
a = 10 / b;
}
finally
{
Console.WriteLine ( "finally" ) ;
}
Console.WriteLine ( "Remaining program" ) ;
Here the output is
My program starts
Exception occurred: System.DivideByZeroException: Attempted to divide by zero.at
ConsoleApplication4.Class1.Main(String[] args) in d:\programs\consoleapplication4\class1.cs:line
51
finally
Note that "Remaining program" is not printed out. Only "finally" is printed which is written in the finally block.
The throw statement throws an exception. A throw statement with an expression throws the exception produced by evaluating the expression. A throw statement with no expression is used in the catch block. It re-throws the exception that is currently being handled by the catch block.
Consider the following program:
int a, b = 0 ;
Console.WriteLine( "My program starts" ) ;
try
{
a = 10 / b;
}
catch ( Exception e)
{
throw
}
finally
{
Console.WriteLine ( "finally" ) ;
}
The output here is:
My program starts
Exception occurred: System.DivideByZeroException: Attempted to divide by zero.at
ConsoleApplication4.Class1.Main(String[] args) in d:\programs\consoleapplication4\class1.cs:line
55
finally
This shows that the exception is re-thrown. Whatever is written in finally is executed and the program terminates. Note again that "Remaining program" is not printed.
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#.