Re: Anyone understood what InnerExceptions are?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



On 29 Mar 2007 04:24:17 -0700, "muler" <mulugeta.abebaw@xxxxxxxxx>
wrote:

Hi all,

Why am I getting a null value to the InnerException object?

Because you haven't set it to anything, so it still has the default
value of null. You need to set it explicitly when you catch and
rethrow.

Can anyone elaborate using examples?

Yes:

static void ThrowSomething() {
throw new Exception("This is the first exception.");
}

static void CatchAndRethrow() {
try {
ThrowSomething();
}
catch (Exception ex) {
// Second parameter is the inner exception
// It preserves details of the initial error.
throw new Exception("This is the rethrown exception.", ex);
}
}

static void Main() {
try {
CatchAndRethrow();
}
catch (Exception ex) {
if (ex.InnerException != null)
Console.WriteLine("Inner Exception: " +
ex.InnerException.ToString());
else
Console.WriteLine(ex.Message);
}
Console.ReadLine();
} // end Main()


rossum


.



Relevant Pages

  • Re: Fastest way to split a file by columns?
    ... ByteBuffer buf = new ByteBuffer; ... // process exception. ... static void initializeStream() throws Exception { ... if (pos>= buf.length) { ...
    (comp.lang.java.programmer)
  • Re: Threading
    ... now I see why I was sceptic about the thread abort exception. ... tried a similar scenario in the default domain. ... static void ThreadProc() ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Check DateTime format
    ... "Jon Skeet " wrote: ... >> format. ... >> inefficient - catching the exception that is. ... static void Time ...
    (microsoft.public.dotnet.general)
  • Re: Check DateTime format
    ... > inefficient - catching the exception that is. ... You can use a regular expression to check the format, ... static void Time ... static readonly Regex Expression = new Regex ...
    (microsoft.public.dotnet.general)
  • Re: ?RE: Could events ever ?overrun? ?or otherwise be missed by a hand?ler?
    ... You can potentially walk the delegate list and invoke by hand which can handle the exceptions and continue calling it... ... Console.WriteLine("Doh, Exception, but continuing."); ... > static void ThrowException() ...
    (microsoft.public.dotnet.framework)