I have a situation where I need to call to foo method, if it fails call bar method, then re-throw the original exception (wrapped).
My problem is that bar method could fail too, and its exception information is important too.
When I create a new Exception there only one cause, and I have two. I could create something like class MyException(cause1, cause2) extends RuntimeException(cause1) but cause2 will be out of the standard mechanisms that exceptions have (stacktrace, etc).
Is there a better way to handle this situation?
The following code is a simplified example.
// I can't change this code
def foo:String = throw new FooException("foo method fails")
def bar:String = throw new BarException("bar method fails")
// my code
try {
foo
} catch {
case error1:FooException =>
try {
// if foo fails, I want to call bar
bar
throw new MyException("my exception", error1)
} catch {
case error2:BarException =>
// but bar could fail too
throw new MyException("my exception", ???)
// if my cause is error1, I lost error2 information (message and stack)
// if my cause is error2, I lost error1 information (message and stack)
}
}
Thank you in advance.