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!!");
}
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.
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.
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.
The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.
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
}
It's quite simple:
What to use actually depends on how much information you want to have about the exception raised.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With