Click here to Skip to main content
15,884,859 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Multiple Try ane One catch are Possible???????
Posted

No.
You can have multiple catch blocks with a single try but you can't have multiple try blocks with a single catch, any more than you can write:
C#
if (a == b)
  {
  DoSomething();
  }
DoAnotherSomething();
else
  {
  DoSomethingElse();
  }
The catch block(s) must immediately follow the end of the try block it applies to, or the system doesn't know what you are trying to do.
 
Share this answer
 
Not directly, but you could sum up the exception handling code in a method like this:
C#
HandleException(Exception e)
{
    // Do whatever seems reasonable with the exception.
}

// Somewhere
try
{
    // Something that throws an exception
}
catch(SophisticatedException ex)
{
    HandleException(ex);
}

// [..]

try
{
    // Some other thing, also throwing an exception
{
catch(SophisticatedException ex)
{
    HandleException(ex);
}
 
Share this answer
 
No you can not use multiple try with one Catch, try using multiple try's and press double tab button it will create try catch block automatically.
 
Share this answer
 
v2

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



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