简体   繁体   中英

Different catch in try-catch

What is difference between these types of catch, except that in first I can use e ?

catch (Exception e)
{
   //some code;
}

catch (Exception)
{
   //some code;
} 

catch
{
   //some code;
} 

Catch can catch different exception's types.
When you use the syntax catch(Exception) you are telling the compiler to write code that catches any kind of exceptions while, if you use a syntax like catch(InvalidOperationException) , you are asking to catch a specific type of exception

To simplify things you can write catch without any type and this has the same meaning of catch(Exception)

try
{
    // Uncomment this line to catch the generic exception
    // throw new Exception("An exception occurred");

    throw new InvalidOperationException("Operation x is not valid in this context");
}
// Comment the following lines to fall into the generic catch exception
catch (InvalidOperationException)
{
    // But without the variable we cannot print out the message....
    Console.WriteLine("An invalid operation has been catched");
}
catch (Exception)
{
    Console.WriteLine("An exception raised");
}

You cannot use the syntax catch(Exception ex) in the same try catch where you don't specify the name of the variable for the same type of exception.

catch (Exception ex)
{
    Console.WriteLine(ex.Message);        
}
// Syntax error: CS0160: A previous catch clause already catches ......
catch (Exception)
{
    Console.WriteLine("An exception raised");
}

Strangely enough this doesn't result in a syntax error, but in a simple warning

catch(Exception)
{
   ....
}
// Warning CS1058: A previous catch clause already catches ......
catch
{
   ....
}

Of course you shouldn't catch exceptions that you are not prepared to handle. If you do it just to expose a message you risk the correct functionality of your program. Usually you catch only specific exceptions that you are know how to handle to allow your program to continue. The only reason that I could find to catch all exceptions is when you write down the exception data in some kind of log file and then throw again the exception.

catch(Exception ex)
{
    Logger.Error("Unexpected exception", ex);
    throw;   // NEVER throw ex;
}

Remember that it is really never required to write throw ex because you loose the stack trace of the exception and make very difficult to track down the exact error point.

See: Best practices for catching and re-throwing .NET exceptions

try{
    //do something
}catch{
    //do something
}

This catch is executed, regardless of the exception.

try{
    //do something
}catch (Exception) {
    //do something
}

This catch is executed when a specific Exception is thrown

try{
    //do something
}catch (Exception e) {
    //do something
}

Same here, only that you have a reference to the Exception. That way, you have access to it.

Read more here .

If your code throws an exception, then the catch Block will be thrown and you have access to it over e .

catch (Exception e)
{
   //some code;
}

If your code throws an exception, then the catch Block will be thrown indepented from the exception type and you don't have access to it.

catch
{
   //some code;
} 

If your code throws an exception, then the catch Block will be thrown depending from the exception type and you don't have access to it.

catch (Exception)
{
   //some code;
} 

Instead of Exception you should use a more specific exception type!

let's check

in this code you can write e.Message for check Catch Message

catch (Exception e)
{
Console.WriteLine("Error Message is : " + e.Message);
}

but in this you just skip From Exception (All Exceptions) and you can add more Exceptions

    catch (sqlExcetion)
    {
    //if your code have sqlEsception Get here 
    }
    catch (Exception)
    {
    //if your code have any Exception Get here
    } 

and in this code you can create one catch and all catch go this

catch
{
   //all catch get here
} 

The minor difference between:

try{
    //do something
}catch (Exception) {
    //do something
}

and

try{
    //do something
}catch (Exception e) {
    //do something
}

is: (the second one will give)

The variable 'e' is declared but never used

Also, if the code is like this:

catch(Exception e) { throw e; }

the original stacktrace is gone. So, you have to do: catch(Exception e) { throw; } catch(Exception e) { throw; } to see the original stacktrace.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM