0

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;
} 
Steve
  • 213,761
  • 22
  • 232
  • 286
gorrch
  • 521
  • 3
  • 16
  • 5
    Please never ever do any of these catches. You should only ever catch specific exceptions that you can handle meaningfully. Otherwise you introduce bugs in your code rather than remove them. Have a read of [Eric Lippert's Vexing Exceptions](https://blogs.msdn.microsoft.com/ericlippert/2008/09/10/vexing-exceptions/). – Enigmativity Sep 09 '18 at 09:49
  • 1
    Main point remains, don't use Try Catch until and unless you plan to do something with an Exception, like logging details, avoid throwing the exception further, since that change the exception call stack (one of the worst programming practice) and always use specific Exceptions instead of generic, to get specific result, simple no rocket science – Mrinal Kamboj Sep 09 '18 at 10:00

5 Answers5

1

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

Steve
  • 213,761
  • 22
  • 232
  • 286
1
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.

some_user
  • 179
  • 11
0

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!

Sean Stayns
  • 4,082
  • 5
  • 25
  • 35
0

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
} 
0

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; } to see the original stacktrace.

Gauravsa
  • 6,330
  • 2
  • 21
  • 30