Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

Naveen...
Posted

Yes it is.

Try something like this.

C#
try
{
     try
   {
   }
   catch(Exception ex)
    {
                MessageBox.Show(ex.Message);
      }
}
catch(SqlException ex)
{
         MessageBox.Show(ex.Message);
}
catch(IOException ex)
{
          MessageBox.Show(ex.Message);
}
catch(Exception ex)
{
           MessageBox.Show(ex.Message);
}
finally
{
       // do you code? :)
}


Should do the trick.
 
Share this answer
 
v2
Tips for exception handling:


1. Don't log the exception in each catch block in the method. I would recommend to log the exceptions at the manager level classes only to avoid flooding of log files.

2. General exception catch should be last catch block to avoid wrapping of known exceptions. (Note that, all exceptions are inherited from "Exception" class)

3. Propagate exceptions to up/top level for logging and handling.

Example:
C#
try
{
    // Add your logic here.
}
catch (IOException ex)
{
    Trace.TraceError("Message={0}\r\nStack={1}", ex.Message, ex.StackTrace);
    throw;
}
catch (Exception ex)
{
    Trace.TraceError("Message={0}\r\nStack={1}", ex.Message, ex.StackTrace);
    throw;
}
finally
{
    // Release resources here.
}
 
Share this answer
 
v2
Comments
Kschuler 29-Jun-10 12:51pm    
Reason for my vote of 5
good tip.
Andrew Rissing 29-Jun-10 17:52pm    
Reason for my vote of 5
Correct usage of 'throw'.
William Winner 29-Jun-10 18:05pm    
Reason for my vote of 5
didn't even see that you had re-thrown the exception.
To propogate exceptions, have a look here.
 
Share this answer
 
Comments
William Winner 29-Jun-10 17:41pm    
Reason for my vote of 2
Do you just post the first google hit you get? That didn't mention anything about multiple catch blocks...and DD had already answered it with a much more useful answer. And it doesn't show how to Throw a new exception.
As written in the previous answer you can write multiple catch blocks after try block with different parameters. Generally in bigger projects. We write custom exceptions for every class/database table.


using throw we can throw the caught exception to the called block.

try
{
//logic
}

catch(Exception ex)
{
throw ;//This exception is thrown to the called block.
}
 
Share this answer
 
Hi all ,

Thanks for answers...

Suppose an exception occurs ,and i want to display a user defined message ,rather than that that exception message ? how do i do that ?

Next does finally {} code runs ,even any catch exception occurs ???


Thanks in advance

Naveen...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900