2

How does the last exception thrown contain all previously thrown exceptions in java?

I read Fail Safe Exception Handling from Fail Safe Exception Handling and I am not able to get this point "One way to do so is to make sure that the last exception thrown contains all previously thrown exceptions." How can we achieve this?

falsarella
  • 12,217
  • 9
  • 69
  • 115

2 Answers2

6

Throwable has two constructors that take another Throwable as the "cause" of this exception:

This can be used to constuct a chain of exceptions.

Error and Exception have similar constructors.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
2

You can construct an exception that contains its cause:

So, you can navigate recursively into the cause, tracking it til the root.

falsarella
  • 12,217
  • 9
  • 69
  • 115