More exception handling questions
Hi guys,
I'm still trying to make sure I am understanding structured exception handling correctly. I have a piece of code to demonstrate my question.
Code:
internal void ReadFromBinary(string PathToFile)
{
try
{
fileAsStream = new FileStream(PathToFile, FileMode.Open);
fileReader = new StreamReader(fileAsStream);
}
catch (FileNotFoundException)
{
throw; //Throw the up to the caller?
}
//Does any code from here down not get called unless the exception is handled?
}
Re: More exception handling questions
the catch will throw the exception
and if there is any codes will be added after the catch statement all these code will be called
Re: More exception handling questions
And if I do not want the code to be called? It hardley seems structured to use a bool and if block after the catch.
Re: More exception handling questions
you can use simple
in your method return; will make you get out of the method when you enter the catch block
Re: More exception handling questions
Whaaat? Noooo.... once an exception is thrown, control is handed back to the calling error handler. So in the case posted, the throw in the catch will re-raise the error, forcing the call to the function to error. If there was an error handler around it, it will then go into that catch. If there isn't then the call to ReadFromBinary will error out. The exception will keep bubbling up the chain until there's code somewhere that handles it... code execution will then resume AT THAT POINT.
-tg
Re: More exception handling questions
amm, lol sorry i'm the one how do the wrong
i didn't see the throw; i think was wrong i must have a look for the question
my answer was if he catch the error and handled it with out the throw
sorry again
Re: More exception handling questions
Yeah thats what I thought but I wasn't sure of which point the code resumed at. Also Between those two lines of code numerous exceptions can occur. Can I simply just handle system.exception when I know that any exception rasied means the function should not continue?
Re: More exception handling questions
all the Exceptions comes from System.Exception
if you have Exception such FileNotFoundException you can handle it with the way you are
if you don't what the exception that will appear
Code:
catch(Exception e)
{
//// do what you want
}