Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I always wrap my code in try...catch blocks? [duplicate]

Possible Duplicate:
When to use try/catch blocks?
Main method code entirely inside try/catch: Is it bad practice?
When to use Try Catch blocks

Exceptions can occur about anywhere, so this made me think: should I always wrap my code in try..catch blocks?

This is for C#.

(I might be missing something fundamental here, as I'm still a newbie)

EDIT: It appears that this was indeed not a very smart question. The only thing we have learnt at school is to use try...catch to prevent crashes. What we did with the exception was showing a MessageBox to tell the user that 'something went wrong when writing this file'.

like image 256
Simon Verbeke Avatar asked Jan 25 '12 17:01

Simon Verbeke


People also ask

Should you wrap everything 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.

Why should I not wrap every block in try catch?

When you have methods that do multiple things you are multiplying the complexity, not adding it. In other words, a method that is wrapped in a try catch has two possible outcomes. You have the non-exception outcome and the exception outcome.

What code is intended to be wrapped in a try block?

The code that we imagine might be problematic, the call to Connect(), is now wrapped in a try block. Any exception that occurs in the code inside the catch block will immediately transfer to the code in the catch block, where we can specify what we want to happen if the call fails.

Is it good practice to write code in catch block?

Only error prone code is within the try block and the handling of that error is written in the catch block. It is used to handle run time exceptions. It is a good practice to write the code in try block which may generate an error , so, that the code doesn't terminate abruptly.


2 Answers

Exceptions can occur about anywhere, so this made me think: should I always wrap my code in try..catch blocks?

Good question. Here's a related question:

Axe-wielding maniacs can be just about anywhere, so: should I wear axe-resistant body armor 24 hours a day?

I am fortunate to live in a neighborhood where the number of axe-wielding maniacs is sufficiently low that I don't wear armor when I leave the house. But suppose I did not. Is the right solution to wear the armor all the time or to jail the maniacs?

If your programs throw so many exceptions that you need to be handling those exceptions everywhere, then you have a big problem. The solution to that problem is not to armor up and put exception handling everywhere. The solution to that problem is to eliminate the code that is throwing the exceptions, and if you cannot eliminate it, then isolate it to a tiny region of code that does use exception handling.

like image 155
Eric Lippert Avatar answered Oct 14 '22 09:10

Eric Lippert


Absolutely not. Here's a great CodeProject article on working with exceptions to get you going.

But more to your point in the OP, Exceptions should only be handled where they need to be handled. This means that a well-implemented application will have a few points in the app (depending on scope of course) where a number of specific, Exception-derived exceptions will be handled and even fewer places (one per thread per a number of best practice suggesttions) where the generic Exception will be handled.

When working with exceptions, don't think in terms of a function returning error information. Exceptions greatly alleviate the tedium of percolating an error condition through your call chain.

like image 27
Paul Sasik Avatar answered Oct 14 '22 07:10

Paul Sasik