Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using catch without arguments

Tags:

c#

try-catch

What is the difference between:

catch
{
    MessageBox.Show("Error.");
}

and:

catch (Exception ex)
{
    MessageBox.Show("Error.");
    //we never use ex, so is it better to use catch without arguments?
}
like image 314
petko_stankoski Avatar asked Nov 04 '11 13:11

petko_stankoski


People also ask

Can we use catch without argument?

Some exception can not be catch(Exception) catched. Below excecption in mono on linux, should catch without parameter. Otherwise runtime will ignore catch(Exception) statment.

Why you should not use try catch?

Without a try catch, you run the risk of encountering unhandled exceptions. Try catch statements aren't free in that they come with performance overhead. Like any language feature, try catches can be overused.

How can you throw outside of a catch block?

If you really need to re-throw an exception outside the catch block, you can use the following method: ExceptionDispatchInfo. Capture(exception). Throw();

Should I put everything in a try catch?

You should not catch any exceptions that you can't handle, because that will just obfuscate errors that may (or rather, will) bite you later on. Show activity on this post. I would recommend against this practice. Putting code into try-catch blocks when you know the types of exceptions that can be thrown is one thing.


4 Answers

As of .NET 2, if you don't tweak the configuration? Nothing.

Before then, or with some config tweak I can't remember precisely, there was the possibility of an exception being thrown from unmanaged code which didn't get converted into an Exception-compatible object.

Note that there's another option in between, where you specify the type but no variable:

catch (Exception)
{
   ...
}

Personally I'd be very wary of catching an exception without even logging it. It may be required if you're calling a boneheaded API, but it's generally best avoided.

like image 64
Jon Skeet Avatar answered Sep 29 '22 18:09

Jon Skeet


I think they are the same. But the second case raised a compiler warning because you declare an exception you didn't use. I rather like the first one because you say explicitly that you don't use the exception. There is also a third one

catch (Exception)
{
    //do something
}

if you want to specify the type of exception but doesn't care about the exception itself.

like image 34
Iesvs Avatar answered Sep 29 '22 20:09

Iesvs


Generally you should catch specific errors first.

But if you go for catching a general Exception like you do I'd say use the second case:

catch (Exception ex)
{
     MessageBox.Show("Error.");
     //we never use ex, so is it better to use catch without arguments?
}

this can help you with debbuging since the variable contains the stack trace, exception message...etc. Which you can use for logging the error or something that will help you preventing it.

Be very carefull using this approach, though:

MessageBox.Show("Error.");

Not keeping track of your errors somewhere(like a log file) can cause a really big mess.

like image 23
TheBoyan Avatar answered Sep 29 '22 18:09

TheBoyan


In your second example you can reference exception data, like the stack trace, source, etc. It also gives a general message that is sometimes helpful. It tells you WHY you suffered an exception which is important when debugging.

like image 34
Jeff LaFay Avatar answered Sep 29 '22 19:09

Jeff LaFay