wood burning stoves*
The moose likes Java in General and the fly likes Throwing multiple exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Throwing multiple exception" Watch "Throwing multiple exception" New topic
Author

Throwing multiple exception

Vijay jai Singh
Greenhorn

Joined: Jan 07, 2009
Posts: 26
Hi,

Is there any way to throw multiple exception instead of just one exception.

Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38793
    
  23
No.

There are however ways of passing Exceptions to each others' constructors so you can throw new Exception(exc); where exc is a different Exception.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Just out of curiosity, why would you want to, and what would doing so mean?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

Nothing prevents you from creating a compound exception. Internally it can store them in a List<Exception>, Exception[] or anything else.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38793
    
  23
But you can't throw a List<Exception> or an Exception[].
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

No but you can throw an instance of an Exception subclass that stores multiple exceptions. Of course printStackTrace would need to be overridden to print the stack traces of all exceptions, and the catching code needs to get the stored exceptions and handle them instead of the wrapping exception.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38793
    
  23
Agree, Rob.

But this is no longer an easy "beginning" question, so I shall move it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Throwing multiple exception