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 have multiple levels of exception throwing (such as when you have nested try-catch blocks). They may be useful if you want to keep the original exception instance, and create a new exception object at the next level, so that at the highest level it is possible to extract the first exception (the inner exception) from the second one.

Going back to the envelope analogy, [14] let's have three people to assist us in our understanding “ Abigail, Betsy, and Charlotte. Abigail writes Betsy a message on a piece of paper and hands it over in an envelope. Betsy can read this message if she wants to, then puts it in a larger envelope with her own message (which may contain her interpretation of Abigail's message). Betsy hands this larger envelope over to Charlotte. Charlotte is able to read Betsy's message, and also Abigail's message in the original form.

[14] I am quite pleased with myself for coming up with this analogy to explain inner exceptions.

Abigail's message is the inner exception which is thrown to Betsy, who creates a new exception with Abigail's exception passed into the constructor. Betsy then throws it to Charlotte.

Of course, inner exceptions make sense only if Betsy has something to add on. If not, she can simply forward Abigail's envelope directly to Charlotte just by using a throw statement. Alternatively, in the case where Betsy doesn't even have to be aware of Abigail's message, Betsy may not even bother to catch it. Under such circumstances, there is really no need (except to mess things up) to use inner exceptions.

You can obtain the inner exception of an exception object via the public InnerException property. The following code example should clarify your understanding of inner exceptions.

 1: using System;  2:  3: class TestClass{  4:   public static void Main(){  5:  6:     try{  7:       DoSomething();  8:     }  9:     catch (Exception e){ 10:       Console.WriteLine(e.Message); 11:       Exception innerE = e.InnerException; 12:       Console.WriteLine(innerE.Message); 13:     } 14:   } 15: 16:   static void DoSomething(){ 17:     try{ 18:       DoSomethingElse(); 19:     } 20:     catch (Exception e){ 21:       // do some local exception processing 22:       throw new Exception("exception in DoSomething",e); 23:     } 24:   } 25: 26:   static void DoSomethingElse(){ 27:     throw new Exception("exception in DoSomethingElse"); 28:   } 29: } 

Output:

 c:\expt>test exception in DoSomething exception in DoSomethingElse 

A new exception is thrown from DoSomethingElse() (line 27) from DoSomethingElse() to DoSomething() at line 18 “ a new exception object is created which encapsulates the original exception as an inner exception (line 22). This new exception object is then thrown back to Main() . In Main() , it is possible to recover the first (inner) exception from the second exception object. Hence, Main() can extract the original (inner) exceptions recursively to obtain every single exception thrown right from the very beginning of the exception chain. It is up to Main() to make use of this information in a creative and useful way.



From Java to C#. A Developers Guide
From Java to C#: A Developers Guide
ISBN: 0321136225
EAN: 2147483647
Year: 2003
Pages: 221
Authors: Heng Ngee Mok

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net