aspose file tools*
The moose likes Java in General and the fly likes Exception Handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Exception Handling" Watch "Exception Handling" New topic
Author

Exception Handling

Samy Ganou
Greenhorn

Joined: Oct 05, 2009
Posts: 28
I have a method like this:

public voide methodA(){

try {
.........

} catch (ExceptionA a){
try{
doSomethingB();
}catch(ExceptionB b){
throw new ExceptionB(b)

}

}

The problem is that when an exception occur on doSomthingB() original stack trace will be lost. No chance to have information about exceptionA.

An idea ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39865
    
  28
That is because you are dealing with the two possible exceptions differently. You might use a single try block with two catch blocks, for ExceptionA and ExceptionB. And why are you throwing an Exception of the same type as you are catching? That looks very peculiar.
Samy Ganou
Greenhorn

Joined: Oct 05, 2009
Posts: 28
Campbell Ritchie wrote:That is because you are dealing with the two possible exceptions differently. You might use a single try block with two catch blocks, for ExceptionA and ExceptionB. And why are you throwing an Exception of the same type as you are catching? That looks very peculiar.


I can not make one try , because i want to call the method doSomethingB() only when an ExceptionA occurs.

I made an error , so i throw ExceptionD() on the second catch.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I'm not exactly sure what you're trying to do, but you still have access to the original exception inside the second exception handler because of normal Java scoping rules. If you need access to it internally then either throw a new exception and chain the original, or provide an exception B constructor that takes an exception A as a parameter.
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525

As David suggests, both exceptions 'aa' and 'bb' are available
within the seond catch block. You should consider using code
indents as it shows parameter scope very clearly.

Jim ... ...


BEE MBA PMP SCJP-6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception Handling