What code gets executed if an exception is thrown? [closed]
try
{
some code
}
catch()
{
some code
}
finally
{
some code
}
try
{
some code
}
catch()
{
some code
}
finally
{
some code
}
I know that if an exception is thrown in the first try block, then the first finally block will be executed. What about the second finally block?
Also, if you want to display the message to user when an exception arises, then where you should write that message, and how you should display it?
FYI, I was recently asked these questions in an interview, and was stumped.
Copy and paste this code into an editor. Then play around with it, uncommenting and re-commenting various lines. Then compile and run the code. Keep doing this until you're pretty comfortable that you know everything about it. This is what I recommend you do when you find all such confusing questions, that are simply based on flow control. That is how you will learn programming flow control.
try
{
Console.WriteLine("try1");
// throw new ArgumentNullException();
// Console.WriteLine(((string)null).Length); // Will also throw exception
}
catch(ArgumentNullException e1)
{
Console.WriteLine("catch1");
Console.WriteLine(e1.ToString());
// throw;
// throw new ArgumentNullException();
// Console.WriteLine(((string)null).Length); // Will also throw exception
}
catch(Exception e1a)
{
Console.WriteLine("catch1a");
Console.WriteLine(e1a.ToString());
// throw;
// throw new ArgumentNullException();
// Console.WriteLine(((string)null).Length); // Will also throw exception
}
finally
{
Console.WriteLine("finally1");
// throw new ArgumentNullException();
// Console.WriteLine(((string)null).Length); // Will also throw exception
}
try
{
Console.WriteLine("try2");
// throw new ArgumentNullException();
// Console.WriteLine(((string)null).Length); // Will also throw exception
}
catch(ArgumentNullException e2)
{
Console.WriteLine("catch2");
Console.WriteLine(e2.ToString());
// throw;
// throw new ArgumentNullException();
// Console.WriteLine(((string)null).Length); // Will also throw exception
}
catch(Exception e2a)
{
Console.WriteLine("catch2a");
Console.WriteLine(e2a.ToString());
// throw;
// throw new ArgumentNullException();
// Console.WriteLine(((string)null).Length); // Will also throw exception
}
finally
{
Console.WriteLine("finally2");
// throw new ArgumentNullException();
// Console.WriteLine(((string)null).Length); // Will also throw exception
}
In the code you wrote, because they are two distinct try blocks (ie one is not contained in the other.) They will both be attempted, the second one after the first finally block is ran. The second finally block will also be ran.
The second one is situationally dependent, generally, you want to keep your exceptions away from the user as much as possible. You want your program to be a butler, quiet, out of the way, but there when you need it. If it were me, I'd probably quietly log the issue, and then continue in the most sane manner possible, unless it was a big issue and you need to notify the user, such as "my ftp client can't find a network connection." If that's the case, and you're in C#, then I'd suggest taking a look at this page: http://msdn.microsoft.com/en-us/library/system.windows.forms.messagebox.aspx
*when I say program blows up, the app dies and raises a runetime exception because it was not caught, and no further code is executed.
try
{
some code1 //always executes this, on exception goto code2
}
catch()
{
some code2 //if exception was caught do this, if exception occurs in this code program blows up
}
finally
{
some code3 //always executes this, if exception happens here, program blows up
}
try
{
some code4 //if program has not blown up at this point, execute this. on exception goto code 5
}
catch()
{
some code5 // if exception was caught do this. if exception occurs in this code, program blows up.
}
finally
{
some code6 //if program has not blown up by now, always do this.
}
Common code paths would be: 1-3-4-6 for a no exceptions found and 1-2-3-4-5-6 if an exception happened in 1 & 4 other cases are rarer (your error handling is broken)
精彩评论