6

I have read and discussed the following questions and articles deeply and many others now and in the past:

When to use try/catch blocks?

Main method code entirely inside try/catch: Is it bad practice?

When to use Try Catch blocks

I will make this article the coding standard of exception handling in my organization! avery nice one but didn't answer me: http://www.codeproject.com/Articles/9538/Exception-Handling-Best-Practices-in-NET

What is the Best practice for try catch blocks to create clean code?

Best practices for exception management in Java or C# Here: I didn't like this statment :( You should not try to catch each and every exception in every possible place.

Should multiple Try/Catch blocks in a method be combined

I have a problem when i need to decide to enclose some block of code with try-catch statement, I know that the code should be enclosed is the faulty code, And i must check what I can check, but for example: I need to write a line in some text file, I should check if the file is exists, and if I have permission to write on it, should I check if there is a space on the disk, or the disk is writable, and if i checked the space, what if something happened while I writing the file (Some other app or thread used the space, or the removable drive has been removed?), Is it a best practice if I checked those things and handled IOException and SecurityException and other potential exceptions, or I should only check without try-catch?

Another example: I am using EntityFramework to access database, when accessing something may contact the database, I know I should check the connection if closed and try to open it, but there are many and many things may cause a failure in this statement, the database may be on a removable drive, this drive may be removed while reading, the service of the DBMS may stop for any reason, no space exception may be thrown, the scheme of the database may change after I try to execute my code for some **** reason,How can I prevent my code from fail, can I just check every thing I can check, and go on? or I should use try catch for exception I can expect even though I have checked them?

Give me references of your answers please, not a general answers!

EDIT

And sure read this : http://msdn.microsoft.com/en-us/library/seyhszts.aspx

Community
  • 1
  • 1
Saw
  • 6,199
  • 11
  • 53
  • 104
  • Please make a comment if you down-voted! – Saw Nov 18 '12 at 12:11
  • You only care why it failed if you can, or are willing to, do the work to correct it in the code. Otherwise you can just log the exception and rethrow. Most apps do not need the kinds of robustness you are proposing, some do. – PatFromCanada Nov 18 '12 at 12:46

2 Answers2

4

Good question! One of your links (Daniel Turini) is one of my favourites. I go back to it again and again when my thinking needs some straightening out.

A good way of expressing my attitude to exception-handling is: only handle exceptions that you can handle. Meaning that you can never work out what to do in response to any possible exception that comes up, and implement this in your code. There are some "expected" exceptions that can be dealt with - but what the code does in response to these is a design decision in itself. IMHO the priority in handling these is not to leave a mess behind after the app shuts down (or at least, backs out of the particular transaction it was involved in) - not for the application to be able to carry on regardless, because shutting down with a graceful "I don't know why, but this exception happened" notification (to a log/IT email/MsgBox, whatever) is somehow a "bad thing".

The direct opposite of this kind of design is the kind of exception-handling that tries to makes the app a kind of "nuclear-Armageddon-survivor". Tries to... I've seen code that responds to IO errors by assuming the network file-server is down, and branches to trying to work, in some undesigned way, on the C: drive. And code that attempts to SELECT FROM ATable: if the table doesn't exist, it CREATES it!!! Every time someone writes code like this, a baby possum dies.

Turini says "never swallow exceptions". What I'm thinking is an extension of this: IMHO you have to be very careful about letting an app carry on, even in response to a known, definite type of exception, with a known cause: because even this constitutes "swallowing" exceptions. Do it, but do it carefully and well.

So in my way of thinking there's always room for a generic "any other case" exception-handler, that delegates the decision up to a higher power (a human being) by logging the details of what happened and just shutting down the app.

My 2c..

sebt
  • 525
  • 3
  • 8
  • I would posit that the proper approach should be to ensure that unexpected exceptions invalidate any data structures that may have become corrupted as a result of the unexpected departure from normal execution sequence. If code can survive without those data structures, it should. If can't, invalidating the data structures will ensure it dies quickly. If code calls `LoadDocument` and gets an exception, it shouldn't have to know all the possible reasons the operation might have failed to know whether it should crash or simply report that the file couldn't be read. – supercat May 14 '13 at 23:01
0

IMHO: Catch every exception in a final layer of your application to prevent your app from being crashed, (In the UI, in the body of your console App, or in the body of some service method... etc) and handle it if you can or at least, log it, But in the internal layers, catch only the exceptions those you just know to handle them perfectly, and throw or wrap the exceptions those with no perfect way to handle, But if your code have no clear boundaries between layers (The same in my case) you should try to handle exceptions whenever it may occur, and try to reset (rewrite) this app (As we will do soon!) in a well constructed way!

Saw
  • 6,199
  • 11
  • 53
  • 104