aspose file tools*
The moose likes Beginning Java and the fly likes Exception Handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception Handling" Watch "Exception Handling" New topic
Author

Exception Handling

Swaranga Sarma
Greenhorn

Joined: Dec 17, 2010
Posts: 9
I need help on this. We know that a finally block will be executed even if no Exceptions are thrown. But how do we check if we are in the finally block after an exception has been thrown or following normal course of program execution. We can use manual methods like setting a boolean flag to true in the catch block and then checking the value of the boolean flag in the finally block; but I am more interested if there are any automatic methods or APIs.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

but I am more interested if there are any automatic methods or APIs.

None. finally should be independent of normal flow and exceptions.


[My Blog]
All roads lead to JavaRanch
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13884
    
  10

Welcome to JavaRanch.

Why would you want to check in the finally block if an exception happened before you got there? You should handle the exception in the catch block, so that you don't have to do anything special for it in the finally block. Can you give a code example that shows why you would want to do this?

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Nuwan Arambage
Ranch Hand

Joined: May 05, 2010
Posts: 76
yes again, it is important to understand why finally block is added to the Java exception handling mechanism.The intent is to execute the code that you need to run irrespective of the normal clause or exceptional clause.

For a example, most of the resource clean up is being done under the final block.I don't know if there is a method for handling you requirement.



Thinker
Nuwan Arambage
Swaranga Sarma
Greenhorn

Joined: Dec 17, 2010
Posts: 9
Since I am not allowed to share the exact code due to legal issues this is more or less what I am trying to do :


I need to do it this way because the "cleanup" code in the finally block may throw additional exceptions. We have to throw them back to the caller function but if there was an exception thrown in the try block those exceptions must take precedence and be thrown instead.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception Handling
 
Similar Threads
help me. confused with exception
try and catch
Exception in java
Catch exceptions (where?)
Try Catch Finally - Voodoo Question