Next [Next] Is it possible for multiple catch inside try block ? if so how ? and how to pass the particular exception to caller? [Resolved]

Posted by Nav234 under Error and Solution on 6/26/2010 | Views : 1959 | Status : [Member] | Replies : 2

Hi all,

This was asked in my recent dot net inetrview .
Either in c # or vb.net ,

While using Try ,catch ,
Is it possible for multiple catch ?

If so

Try ,
````
`````
````
Catch 1
Catch 2
Catch 3

And if a catch occurs ,how to log that particular exception and pass
how to Pass it to that method Caller().?

Thanks in advance

S.Naveen...


Responses

Posted by: Abhi2434 on: 6/26/2010 [Member] [Microsoft_MVP] [MVP] Silver

Up
0
Down

Resolved
Are you looking for something like this :

 try

{

return (Earth)planets[index];
}
catch (InvalidCastException ex)
{
Debug.Write(ex.Message);
}
catch (IndexOutOfRangeException ex)
{
Trace.TraceError(ex.Message);
}
catch (Exception ex)
{
throw ex;
}


You can create as many catch as you want, but make sure you place the one exception which absorbs the most as last catch. Here Exception as being the base, will absorb all exception, so if you place Exception e as first catch, no other catch block will execute. So I placed Exception ex as the last catch as catches are matched sequentially.

www.abhisheksur.com

Nav234, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nav234 on: 6/27/2010 [Member] Starter

Up
0
Down
Thanks Mr.Abhisheksur...i got it ...

S.Naveen...

Nav234, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response