I have a question about the usage of throw in the following code I found on msdn.
In this code, an excpetion is caught using try catch block and in the catch block it is rethrown using an explicit throw statement.
My question is that who handles the explicitly thrown exception
The same catch if this is the case then the compiler will be trapped
infinitely inside the catch block.
using System;
using System.IO;
public class ProcessFile
{
public static void Main()
{
FileStream fs = null;
try
//Opens a text tile.
{
fs = new FileStream("data.txt", FileMode.Open);
StreamReader sr = new StreamReader(fs);
string line;
//A value is read from the file and output to the console.
line = sr.ReadLine();
Console.WriteLine(line);
}
catch(FileNotFoundException e)
{
Console.WriteLine("[Data File Missing] {0}", e);
//Who handles the following throw statement
throw new FileNotFoundException("[data.txt not in c:\\dev directory]",e);
}
finally
{
fs.Close();
}
}
}
who catches explicity thrown exception ?
AhXue
arogan
If the ProcessFile class is being consumed by another class and the call to ProcessFile.Main() is encapsulated in a try...catch...[finally], then the thrown exception will be bubbled up to the caller and handled in the calling catch block.
Good luck!
Ethan
Roni Schuetz
hommer
Take a closer look at the event i gave you in the MSDN. It has some remarcks and also some code explaining how to do what i suggested.
Just read this:
http://msdn2.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx
Mike Wachal - MSFT
Hello
In this example the Exception will cause to program to crash. It will not be caught by the catch block. If you throw an exception inside a catch block, you want the code thats "above you" handle it. In this example there is no "above" so it will cause the Program to fail.
What you COULD do, if you want to catch ALL Exceptions and keep running, is to catch all
AppDomain.CurrentDomain.UnhandledException
Events and process them...
Dreedle
"code thats "above you" "
that would catch the explicit throw
Do you mean there should be another catch block inside the Main()
If this is true then how would a compiler know which catch block will handle this explicit throw exception in case there are more than one catch in the same Main() function
Vince P
This isn't always the case, see the example below:
public class WorkClass()
{
public void DoWork()
{
{
catch (Exception e)
{
finally
{
...
public static class MainClass() // Program Entry
{
{
try
{
workWidget.DoWork();
{
finally
{
...
In the example above, the exception encountered in WorkClass isn't handled per se in it's catch block, but rethrown. If the caller implements a try...catch around the call to WorkClass, then the exception will be bubbled up to the caller and handled in the calling catch block. I just wanted to clarify this all for the OP.
As you know and noted, if the caller doesn't handle the exception the system will thrown an unhandled exception error.
CodeButcher
Hi, Ethan
Excuse me for not describing clearly. What I mean is that in the case no try block logically outside the catch block, it will be handled by system. Cause no catch block in the program follows.
Hope it clearer
deGame
As a side note, when rethrowing an exception you do not generally want to do:
try
{
// Do Work
}
catch (Exception e)
{
throw e;
}
but rather:
try
{
// Do Work
}
catch (Exception e)
{
throw;
}
Otherwise you will lose the original stacktrace of the exception, as "throw e" will update the original exceptions stacktrace to appear as if it originated from the method it is inside, not from where it originally came from.
Mark.
x868
A common usage of catch and finally together is to obtain and use resources in a try block, deal with exceptional circumstances in a catch block, and release the resources in the finally block.
If the code in try block cause exception it throw to the catch, and if throw from the catch block, the exception is supposed to handled by the system (in case no catch block in the program follows). And whatever exception occured the finally block is sure to be excuted anyway.
The example it shows is to demonstrate how to explicitly throw an exception and further tell you that "it is good coding practice to add information to an exception that is rethrown to provide more information when debugging".