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
-
Help me how to dynamic create row column of TableLayoutpanel at run time ??????
by anatha1 (0 replies)
-
Very Urgent regarding deleting the images from a folder
by rameshbandi (2 replies)
-
How to Export Datagridview contents to Excel
by BarbaMariolino (8 replies)
-
Help accessing sound card
by daz4904 (0 replies)
-
How to Write a GPS Application
by stoyac (19 replies)
Related podcasts
-
Object-Oriented Programming in Ruby
In this episode, I talk with Scott Bellware about object-oriented programming in Ruby, and Ruby's object model. This is taken from a private conversation, and the audio quality suffers at times. Much thanks to Scott for allowing this to be released.This episode of the Alt.NET Podcast is bro...
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#.