473,396 Members | 1,970 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Finally....another exception?

Hi.
What would happen if an exception occurs inside a Finally block and at the
same time inside the try another exception was thrown without been handled
by any catch?

Alejandro.
Nov 16 '05 #1
2 1408
It would throw the new exception and the original exception would not be
seen. Given the code...

try
{
throw new Exception("1st exception");
}
catch(Exception ex1)
{
// catches 1st exception
}
finally
{ // runs after the catch block catches the 1st exception
throw new Exception("2nd exception");
}

the 2nd exception is not caught in this snippet, and unless it gets caught
somewhere further back up the call stack it would eventually result in an
unhandled exception.

If the entire snippet were enclosed in a surrounding try-catch block, then
the outermost catch would catch the 2nd exception - the 1st exception gets
dropped.

"Alex" <al********@hotmail.com> wrote in message
news:uw**************@TK2MSFTNGP11.phx.gbl...
Hi.
What would happen if an exception occurs inside a Finally block and at the
same time inside the try another exception was thrown without been handled
by any catch?

Alejandro.

Nov 16 '05 #2
Thanks. Here is the situation I was thinking about:

try
{
throw new Exception("1st exception");
}
catch(AnotherException ex1)
{
// DON'T catches 1st exception
}
finally
{ // runs after the catch block DON'Tcatches the 1st exception
throw new Exception("2nd exception");
}

Alejandro.

"David Levine" <no****************@wi.rr.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
It would throw the new exception and the original exception would not be
seen. Given the code...

try
{
throw new Exception("1st exception");
}
catch(Exception ex1)
{
// catches 1st exception
}
finally
{ // runs after the catch block catches the 1st exception
throw new Exception("2nd exception");
}

the 2nd exception is not caught in this snippet, and unless it gets caught
somewhere further back up the call stack it would eventually result in an
unhandled exception.

If the entire snippet were enclosed in a surrounding try-catch block, then
the outermost catch would catch the 2nd exception - the 1st exception gets
dropped.

"Alex" <al********@hotmail.com> wrote in message
news:uw**************@TK2MSFTNGP11.phx.gbl...
Hi.
What would happen if an exception occurs inside a Finally block and at the same time inside the try another exception was thrown without been handled by any catch?

Alejandro.


Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Brian Alexander | last post by:
Hello; I'm curious to know how people preserve exceptions that arise in a try ... finally block. Consider this example: try: getResource() doSomething() finally: alwaysFreeResource()
12
by: Brian Kelley | last post by:
def res(): try: a = 1 return finally: print "do I get here?" res() outputs "do I get here?"
8
by: Z D | last post by:
Hi, I was wondering what's the point of "finally" is in a try..catch..finally block? Isn't it the same to put the code that would be in the "finally" section right after the try/catch block?...
16
by: Chris | last post by:
Hi, regarding exception-handling : why put code in a 'finally' block, and not just after a 'catch'-block --> Example using 'finally ' : try { OpenFile();
6
by: Tilfried Weissenberger | last post by:
Hi, I am a bit confused as to what the FINALLY block is meant for. What's the difference between: this.Cursor = Cursors.WaitCursor; try { //do some stuff } catch { //handle exception }...
24
by: Dave | last post by:
Maybe I'm missing something here, but I can't see the purpose of the 'finally' keyword. What is the difference between: try { doSomething() } catch { handleError(); }
7
by: Sean Kirkpatrick | last post by:
I got caught with my pants down the other day when trying to explain Try...Catch...Finally and things didn't work as I had assumed. Perhaps someone can explain to me the purpose of Finally. I've...
32
by: cj | last post by:
Another wish of mine. I wish there was a way in the Try Catch structure to say if there wasn't an error to do something. Like an else statement. Try Catch Else Finally. Also because I...
13
by: Jon Davis | last post by:
I understand that the finally sub-block will execute regardless of whether try succeeded or not. But so will code that follows try...catch. So then what is the difference between ... try {...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.