Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Try Catch blocks inside or outside of functions and error handing

This is more a general purpose programming question than language specific. I've seen several appraoches to try and catches.

One is you do whatever preprocessing on data you need, call a function with the appropriate arguments and wrap it into a try/catch block.

The other is to simply call a function pass the data and rely on try catches within the function, with the function returning a true/false flag if errors occured.

Third is a combination with a try catch outside the function and inside. However if the functions try catch catches something, it throws out another exception for the try catch block outside the function to catch.

Any thoughts on the pros/cons of these methods for error control or if there is an accepted standard? My googling ninja skills have failed me on finding accurate data on this.

like image 524
CogitoErgoSum Avatar asked Jan 22 '10 19:01

CogitoErgoSum


People also ask

Can we handle error in catch block?

Catching Errors lang. Error class in Java doesn't inherit from java. lang. Exception, we must declare the Error base class – or the specific Error subclass we'd like to capture – in the catch statement in order to catch it.

What is try catch error handling?

The try... catch construct allows to handle runtime errors. It literally allows to “try” running the code and “catch” errors that may occur in it. The syntax is: try { // run this code } catch (err) { // if an error happened, then jump here // err is the error object } finally { // do in any case after try/catch }

Which of the following is a error function used with catch block?

In the scope of a CATCH block, the following system functions can be used to obtain information about the error that caused the CATCH block to be executed: ERROR_NUMBER() returns the number of the error. ERROR_SEVERITY() returns the severity. ERROR_STATE() returns the error state number.

Can we use try catch inside catch?

Yes, we can declare a try-catch block within another try-catch block, this is called nested try-catch block.


2 Answers

In general, an exception should only be caught if it can actually be handled.

It makes no sense to catch an exception for no purpose other than to log it. The exception is that exceptions should be caught at the "top level" so that it can be logged. All other code should allow exceptions to propagate to the code that will log them.

like image 131
John Saunders Avatar answered Sep 28 '22 11:09

John Saunders


I think the best way to think about this is in terms of program state. You don't want a failed operation to damage program state. This paper describes the concept of "Exception Safety".

In general, you first need to decide what level of exception safety a function needs to guarantee. The levels are

  • Basic Guarnantee
  • Strong Guarantee
  • NoThrow Guarantee

The basic Guarantee simply means that in the face of an exception or other error, no resources are leaked, the strong guarantee says that the program state is rolled back to before the exception, and nothrow methods never throw exceptions.

I personally use exceptions when an unexpected, runtime failure occurs. Unexpected means to me that such a failure should not occur in the normal course of operations. Runtime means that the error is due to the state of some external component outside of my control, as opposed to due to logic errors on my part. I use ASSERT()'s to catch logic errors, and I use boolean return values for expected errors.

Why? ASSERT isn't compiled into release code, so I don't burden my users with error checking for my own failures. That's what unit tests and ASSERTS are for. Booleans because throwing an exception can give the wrong message. Exceptions can be expensive, too. If I throw exceptions in the normal course of application execution, then I can't use the MS Visual Studio debugger's excellent "Catch on thrown" exception feature, where I can have the debugger break a program at the point that any exception is thrown, rather than the default of only stopping at unhandled (crashing) exceptions.

To see a C++ technique for the basic Guarantee, google "RAII" (Resource Acquisition is Initialiation). It's a technique where you wrap a resource in an object whose constructor allocates the resource and whos destructor frees the resource. Since C++ exceptions unwind the stack, it guarantees that resources are freed in the face of exceptions. You can use this technique to roll back program state in the face of an exception. Just add a "Commit" method to an object, and if an object isn't committed before it is destroyed, run the "Rollback" operation that restores program state in the destructor.

like image 45
David Gladfelter Avatar answered Sep 28 '22 11:09

David Gladfelter