13.4. Inner exceptions

C# came out with something called an inner exception – effectively one exception encapsulated within another. [13]

[13] Inner exceptions can be implemented in Java too, but it requires extra manual coding effort. In C#, support for inner exceptions is deeply 'entrenched' high up in the exception hierarchy in System.Exception.

System.Exception's third constructor shown in Table 13.3 enables you to pass in another exception object to the constructor. Think of it as stuffing a smaller envelope with a message into a bigger envelope (which may contain its own message too). To create an new exception object which encapsulates an inner exception object, use the third constructor in Table 13.3.

Inner exceptions are useful when you ...

Get From Java to C#: A Developer's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.