who catches explicity thrown exception ?  
Author Message
bilalso





PostPosted: Visual C# Language, who catches explicity thrown exception ? Top

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();
}
}
}


Visual C#16  
 
 
Hatzi74





PostPosted: Visual C# Language, who catches explicity thrown exception ? Top

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...



 
 
littleguru





PostPosted: Visual C# Language, who catches explicity thrown exception ? Top

This exception isn't catched again in the same catch block. It's thrown and is catched by other code that wraps around the try { ... } catch(FileNotFoundException e) { ... } finally { ... } block. Since this is here in the main method and I don't see any other try catch block this exception isn't catched and the user gets an error message - the program will probably crash then.

 
 
bilalso





PostPosted: Visual C# Language, who catches explicity thrown exception ? Top

Can you please give an example of
"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

 
 
ahmedilyas





PostPosted: Visual C# Language, who catches explicity thrown exception ? Top

I agree with what most people have said here about the app crashing. Usually the caller calling the code will catch the exception that was thrown from the code it was executing, but if there were no try catch blocks around that to catch the rethrown exception then you will get a runtime error/exception thrown

 
 
Hatzi74





PostPosted: Visual C# Language, who catches explicity thrown exception ? Top

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



 
 
Ethan Pack





PostPosted: Visual C# Language, who catches explicity thrown exception ? Top

If the ProcessFile class is not being consumed elsewhere, the newly thrown exception will cause an Unhandled Exception error to occur (dialog box at runtime explaining the exception.)

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

 
 
Figo Fei - MSFT





PostPosted: Visual C# Language, who catches explicity thrown exception ? Top

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".



 
 
Ethan Pack





PostPosted: Visual C# Language, who catches explicity thrown exception ? Top

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.

This isn't always the case, see the example below:

public class WorkClass()
{
public WorkClass() {}
public void DoWork()
{
try
{
// Do Work
}
catch (Exception e)
{
throw e;
}
finally
{
// Clean up regardless
}
}
...
}

public static class MainClass() // Program Entry
{
public static void Main()
{
WorkClass workWidget;
try
{
workWidget = new WorkClass();
workWidget.DoWork();
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
workWidget = null;
}
}
...
}

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.

 
 
Figo Fei - MSFT





PostPosted: Visual C# Language, who catches explicity thrown exception ? Top

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

 
 
Mark Dawson





PostPosted: Visual C# Language, who catches explicity thrown exception ? Top

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.