It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Exception chaining and handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception chaining and handling" Watch "Exception chaining and handling" New topic
Author

Exception chaining and handling

Elena Taube
Greenhorn

Joined: Dec 06, 2005
Posts: 21
Hello everyone,

I have never make in a practice the exception handling and i have no any experience with it. I have some questions about exception chaining and handling could you help me?



In this example I catch Exception1 and rethrow the Exception2. Since JDK 1.4 there is a possibility to add to the Exception2 a cause of the Exception1. But If I didn't throw the cause of the Exception1 is this also an exception chaining? Or the meaning of chaining is only if the cause (Throwable) of one exception will be thrown with the cause of another.

Another example. How is a best way to rethrow an exception in practice?

or



If I have e.g. three calls of different methods that catch one exception and rethrow the another one. Do I need to rethrow the cause of these exceptions every time and log at the end point all exceptions :

or just log the occurred exception before throwing and throw only the message?
How it will be made in a real applications?
Thanks a lot for your help!
C Broussard
Greenhorn

Joined: Jun 18, 2004
Posts: 28
well what you're talking about is checked & unchecked exceptions.

unchecked exceptions should extend RuntimeException (they flow all the way up to the top of the stack unless you specifically catch it and do something w/ it) this is what the springframework uses.


checked exceptions are caught and handled with.

it is always preferred to do catch them and keep the original message

}catch(Exception e){
throw new BusinessLogicException(e);
}

... safe rule of thumb is to have business logic exceptions checked, and handled for whatever reason or however.. and system problems (sql, dataccess, etc unchecked).

hope that helps


www.binaryfrost.com
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Exception chaining and handling
 
Similar Threads
Barry Boone - Question #28 - Ambiguous
code problem.help please!!!!
Exception handling
Why this is a checked exception?
exception