Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Catch(Exception) and Catch(Exception ex)

What is the difference between Catch(Exception) and Catch(Exception ex) . I can see both giving me expected output. Then what is the actual difference ? Which one is recommended ?

Suppose the code is below.

int a = 1, b = 0;
try
{
    int c = a / b;
    Console.WriteLine(c);
}

Which of the below catch block is recommended to use ? What is the actual difference between those ?

catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}

OR

catch (Exception)
{
    Console.WriteLine("Oh NO!!");
}
like image 286
skjcyber Avatar asked Aug 11 '13 14:08

skjcyber


People also ask

What is catch exception ex?

Catch (Exception ex) catches all exceptions and in addition you can retrieve message through its reference. Use is dependent on requirement, if you want to show exception message you have facility to use ex. Message otherwise Catch (Exception) will be enough.

What is ex in exception in Java?

An exception (or exceptional event) is a problem that arises during the execution of a program. When an Exception occurs the normal flow of the program is disrupted and the program/Application terminates abnormally, which is not recommended, therefore, these exceptions are to be handled.

What is ex in exception C#?

Figure 2: C# program with throw ex It accepts an integer parameter and divides that number by zero. In the main program, the DivideByZero method is called inside the try block. It causes an exception and the catch block is executed. As the catch block has the throw ex keywords, it will throw an exception.

What is the purpose of the catch exception ex ){} block?

The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.


2 Answers

Well, catch(Exception ex) is just the same as catch(Exception) with one difference only: in catch(Exception ex) we have an access to the exception class (error cause) instance. Usually you need an exception class instance to print out the original message:

  try {
    ...
  }
  catch (AppServerException e) {
    Console.WriteLine("Application server failed to get data with the message:");
    Console.WriteLine(e.Message); // <- What's actually got wrong with it
  }

If you don't need the exception class instance, e.g. you plan just to consume the exception, the catch(Exception ex) syntax is excessive and catch(Exception) is prefferable:

  try {  
    c = a / b;
  }  
  catch (DivideByZeroException) {
    c = Int.MaxValue; // <- in case b = 0, let c be the maximum possible int
  }

Finally. Do not catch general Exception class without re-throughing:

  try {
    int c = a / b;
  }
  catch (Exception) { // <- Never ever do this!
    Console.WriteLine("Oh NO!!");
  }

do you really want to code "whatever error (green fume from CPU included) had happend just print out "Oh No" and continue"? The pattern with Exception class is something like this:

  tran.Start();

  try {
    ...
    tran.Commit();
  }
  catch (Exception) {
    // Whatever had happened, let's first rollback the database transaction
    tran.Rollback();

    Console.WriteLine("Oh NO!");

    throw; // <- re-throw the exception
  }
like image 108
Dmitry Bychenko Avatar answered Sep 26 '22 03:09

Dmitry Bychenko


It's quite simple:

  • in the first code, you can catch the exception and get the object representing it so you can have more information about what happened
  • in the second code, you only know that an exception has been raised, but you don't have more information about it.

What to use actually depends on how much information you want to have about the exception raised.

like image 40
ppetrov Avatar answered Sep 26 '22 03:09

ppetrov