<p>Is there a way to detect, from within the finally clause, that an exception is in the process of being thrown?</p>

<p>See the example below:</p>

<pre><code>
try {
    // code that may or may not throw an exception
} finally {
    SomeCleanupFunctionThatThrows();
    // if currently executing an exception, exit the program,
    // otherwise just let the exception thrown by the function
    // above propagate
}
</code></pre>

<p>or is ignoring one of the exceptions the only thing you can do?</p>

<p>In C++ it doesn't even let you ignore one of the exceptions and just calls terminate(). Most other languages use the same rules as java.</p>

[{'answer_id': 66664, 'author': 'zxcv', 'author_id': 9628, 'author_profile': 'https://Stackoverflow.com/users/9628', 'pm_score': -1, 'selected': False, 'text': '<p>No I do not believe so.  The catch block will run to completion before the finally block.</p>\n\n<pre><code>try {\n    // code that may or may not throw an exception\n} catch {\n// catch block must exist.\nfinally {\n    SomeCleanupFunctionThatThrows();\n// this portion is ran after catch block finishes\n}\n</code></pre>\n\n<p>Otherwise you can add a synchronize() object that the exception code will use, that you can check in the finally block, which would help you identify if in a seperate thread you are running an exception.</p>\n'}, {'answer_id': 66673, 'author': 'ReaperUnreal', 'author_id': 4218, 'author_profile': 'https://Stackoverflow.com/users/4218', 'pm_score': 1, 'selected': False, 'text': "<p>If a function throws and you want to catch the exception, you'll have to wrap the function in a try block, it's the safest way. So in your example:</p>\n\n<pre><code>try {\n    // ...\n} finally {\n    try {\n        SomeCleanupFunctionThatThrows();\n    } catch(Throwable t) { //or catch whatever you want here\n        // exception handling code, or just ignore it\n    }\n}\n</code></pre>\n"}, {'answer_id': 66705, 'author': 'Chris B.', 'author_id': 9161, 'author_profile': 'https://Stackoverflow.com/users/9161', 'pm_score': 5, 'selected': True, 'text': '<p>Set a flag variable, then check for it in the finally clause, like so:</p>\n\n<pre><code>boolean exceptionThrown = true;\ntry {\n   mightThrowAnException();\n   exceptionThrown = false;\n} finally {\n   if (exceptionThrown) {\n      // Whatever you want to do\n   }\n}\n</code></pre>\n'}, {'answer_id': 66706, 'author': 'Rob Dickerson', 'author_id': 7530, 'author_profile': 'https://Stackoverflow.com/users/7530', 'pm_score': 0, 'selected': False, 'text': "<p>Do you mean you want the finally block to act differently depending on whether the try block completed successfully?</p>\n\n<p>If so, you could always do something like:</p>\n\n<pre><code>boolean exceptionThrown = false;\ntry {\n    // ...\n} catch(Throwable t) {\n    exceptionThrown = true;\n    // ...\n} finally {\n    try {\n        SomeCleanupFunctionThatThrows();\n    } catch(Throwable t) { \n        if(exceptionThrown) ...\n    }\n}\n</code></pre>\n\n<p>That's getting pretty convoluted, though... you might want to think of a way to restructure your code to make doing this unnecessary.</p>\n"}, {'answer_id': 66740, 'author': 'Tim Frey', 'author_id': 1471, 'author_profile': 'https://Stackoverflow.com/users/1471', 'pm_score': 3, 'selected': False, 'text': '<p>If you find yourself doing this, then you might have a problem with your design.  The idea of a "finally" block is that you want something done regardless of how the method exits.  Seems to me like you don\'t need a finally block at all, and should just use the try-catch blocks:</p>\n\n<pre><code>try {\n   doSomethingDangerous(); // can throw exception\n   onSuccess();\n} catch (Exception ex) {\n   onFailure();\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/66643', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5963/']