What types of code blocks should I conclude with a try-catch expression?

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?

The main method code is completely inside try / catch: is this a bad practice?

When to Use Try Catch Blocks

I will make this article an exception handling coding standard in my organization! very good, but did not answer: 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?

Guidelines for managing exceptions in Java or C # Here: I did not like this expression :( You should not try to catch every exception in all possible places.

Use multiple Try / Catch blocks in a method

I had a problem when I need to take part in a block of code with a try-catch instruction, I know that the code must be enclosed in a trouble code. And I have to check that I can check, but, for example: I need to write a line in some text file, I have to check if the file exists, and if I have write permission to it, I have to 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 writing the file (did some other application or stream use the space or the removable disk was deleted?). Is it best practice if I checked these things and handled IOException and SecurityException and other potential exceptions, or should I only check without try-catch?

Another example: I use EntityFramework to access the database, when access to something can be connected to the database, I know that I should check the connection if it is closed and try to open it, but there are many, many things. which may cause a failure in this statement, the database may be on a removable disk, this disk may be deleted while reading, the DBMS service may be stopped for any reason, a space exception may not be selected, the database schema may change after of how I try to execute my code for some *** * reason, How can I prevent my code because of a failure, can I just check everything that I can check, and continue? or should I use try catch for an exception that I can expect, although I checked them?

Give me your answers, please do not general answers!

EDIT

And be sure to read the following: http://msdn.microsoft.com/en-us/library/seyhszts.aspx

+6
source share
2 answers

Good question! One of your links ( Daniel Turini ) is one of my favorites. I come back to him again and again when my thinking needs some rectification.

A good way to express my attitude to exception handling is to handle exceptions that can be handled . This means that you can never decide what to do in response to any possible exception that appears, and implement this in your code. There are some "expected" exceptions that can be dealt with - but what the code does in response to them is a very constructive solution. IMHO, the priority in processing them is not to leave the mess after the application is closed (or at least back from the specific transaction in which it was involved) - not to allow the application to continue, despite the fact that closing with an elegant “I don’t know why, but this exception occurred” notification (for the journal / IT email address / MsgBox, regardless) somehow “bad”.

The exact opposite of this type of design is the kind of exception handling that tries to make the application a kind of "nuclear armageddon survivor." Trying ... I saw code that responds to I / O errors, assuming that the network file server is turned off, and branches to try to work, in some unknown way, on the C: drive. And the code that SELECT FROM ATable is trying: if the table does not exist, it CREATES it !!! Every time someone writes such a code, the possum child dies.

Turini says: "never swallow exceptions." I think this is an extension of this: IMHO, you have to be very careful that the application continues, even in response to a known, specific type of exception, with a known reason: because even this constitutes “swallowing” of exceptions, do it, but do it careful and good.

Thus, in my way of thinking, there is always room for a universal “any other case” exception handler that delegates this solution to a higher power (person), recording information about what happened and just closing the application.

My 2c ..

+4
source

IMHO: catch all exceptions at the last level of your application to prevent your application from breaking up (in the user interface, in the shell of your console application or in the body of some service method ... etc.) and handle it if you can or at least register it, but in the inner layers, catch only those exceptions that you just know to handle them perfectly, and throw or end exceptions that don't have an ideal way to handle, but if your code has no clear the boundaries between the layers (the same in my case), you should try to handle exceptions whenever this can happen, and try to reset (rewrite) this application (as we will do in the near future!) in a well-constructed form!

0
source

All Articles