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’.
Good question. Here’s a related question:
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.