• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Throwing multiple exception

 
Vijay jai Singh
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

Thanks
 
Campbell Ritchie
Sheriff
Pie
Posts: 50189
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just out of curiosity, why would you want to, and what would doing so mean?
 
Rob Spoor
Sheriff
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nothing prevents you from creating a compound exception. Internally it can store them in a List<Exception>, Exception[] or anything else.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50189
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you can't throw a List<Exception> or an Exception[].
 
Rob Spoor
Sheriff
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50189
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree, Rob.

But this is no longer an easy "beginning" question, so I shall move it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic