How can I throw cached exception in PL/SQL?
For example I have procedure, where I catch all exceptions:
EXCEPTION
WHEN OTHERS THEN
rollback;
and then I want to throw catched exception to procedure caller.
Thanks in advance!
Just add raise;:
EXCEPTION
WHEN OTHERS THEN
rollback;
raise;
To re-raise the exception, just use
raise;
To define a custom application error, look at raise_application_error, e.g.
raise_application_error(-20001, 'Warp core implosion imminent', true);
It's worth bearing in mind that due to what I've just decided to call the Exception Handling Uncertainty Principle, there is always a trade-off between reporting the full exception details and doing something about the exception.
Related
Is writing one rescue clause at the end of a program to end the program in Eiffel enough to handle exceptions such as pre, postconditions or invariant violations in any of the routines written in the program? Or should I write rescue clause for every function having pre and postconditions to handle exceptions?
I have read Eiffel documentation on error handling but I couldn't figure out.
The answer depends on your expectations. The method is as follows:
Precondition violations are reported to the caller. They indicate a bug in the caller. If bugs are expected or possible in the callers, you can catch and handle them in the callers.
Postcondition violations are reported to the callee. They indicate bugs in the callee. The callee can catch and handle them if such bugs are expected.
Class invariants are established at object creation. If they are violated by the creation procedure or after the execution of a procedure used in a qualified call, they indicate a bug in the callee and can be handled like postcondition violations. Otherwise, they indicate more complex issue involving object dependencies. It could be handled by the caller, but most likely it would be next to impossible to restore correct object state.
In all cases, assertion violations mean a program bug and whether and how it should be caught and handled depends on your needs. E.g., it's possible to do the handling somewhere between the problematic code and the root procedure.
A coworker and I are having a disagreement on the best practice for handling run-time exceptions thrown from transactional JDBC code. He feels that catching Exception rather than SQLException is the proper practice.
His argument is that Connection.close(); has an undefined behaviour if the transaction has not been committed. Some JDBC implementations may do a rollback, while others may do a commit. If you just catch the SQLException, then any runtime exception will have undefined behaviour on the close() in the finally block.
Is it currently considered best practice to catch Exception, and if not, how do you address the situation he describes?
Connection connection = null;
try {
connection = ...
// transactional code
} catch (final Exception e) {
connection.rollback();
} finally {
// close other resources
if (connection != null) { connection.close(); }
}
I would catch Exception too and even Throwable. It all depends on the code inside your block.
Ex:
If you call another DAO to persist another entity, you will for sure catch any kind of exception to make sure you rollback your trx.
You have to ask this question to yourself : do i want to commit the trx even if some non SQLException occur?
From my experience, my answer would be No! ;-)
But that's just me.
Regards
As per Javadoc of java.sql.Connection, behaviour of close is implementation dependent.
It is strongly recommended that an application explicitly
commits or rolls back an active transaction prior to calling the
close method. If the close method is called
and there is an active transaction, the results are implementation-defined.
Its safe to catch and take decision to commit / rollback on basis of business requirement
I have a huge pascal code compiled with fpc.
I am getting random "EInOutError" exception and I am trying to debug it using gdb.
I already tried break fpc_raiseexception, catch throw, catch exception, catch catch. Nothign seems to work...
How can I break (to have a backtrace at gdb) when a exception is raisen?
A breakpoint on fpc_raiseexception should be a good thing.
Don't use gdb "catch" functionality relating to exceptions. Those are for GCC C++ exceptions, not for FPC.
what will the program behave when they have two exceptions.
And none of them have been caught yet.
what type of handler will be called .
lets say both the exceptions were of different type.
i apologize if i am not clear but i feel i have made myself clear enough.
thank you!!!
what if the try block throws an exception and try block is exited which destroyes all the automatic variables.Lets say one was an automatic object and its destructor again threw an exception.Now we have two uncaught exception.My question is based on this fact.
thank you!!
It depends entirely on the language. However, in all the languages I know there can't ever be multiple exceptions at the same time (in the same thread). If an exception has been thrown, it travels up the call stack until it's caught, with no code executing during this time. If the exception is not caught, the program crashes before another can be thrown. If it is caught, the exception is no longer "active" and if the handler throws a new exception, the old one is forgotten.
At the CPU level (on x86), there is a situation called a double fault:
On the x86 architecture, a double fault exception occurs if the processor encounters a problem while trying to service a pending interrupt or exception.
However, this kind of "double fault" is a very low level situation and is only of concern to the operating system kernel.
Few languages or frameworks can do a good job of handling an exception that occurs while cleaning up from an earlier exception. Effective handling of such situations would require the cleanup code to know what exception, if any, occurred on the "main line". Conceptually it would not be at all difficult for such information to be provided to cleanup code, but frameworks generally don't provide it.
Otherwise, normal behavior in C++ is to hard-crash the system when an exception occurs during the cleanup from another exception; Java and .NET languages generally abandon any pending exception if a cleanup exception occurs. Newer versions of Java, however, include a feature which (if used) will handles such things much better. In a try-with-resources block, an exception which occurs while cleaning up resources when no other exceptions are pending will be treated normally; if, however, an exception was pending, the pending exception will remain pending but have the new exception added to its list of "suppressed exceptions". It would be nice if there were a way of specifying that a particular finally block should behave the same way, but I don't know of any feature for that.
When Exception is occurred then complier unwind stack(closing current functions and going to back, caller function). If no exception is caught in main than complier called abort function. By this program terminate abnormally.
But during unwinding stack if another exception is occurred(for your case in destructor) than at this time/point without reaching main function, compiler called abort function which terminate program abnormally.
If you know that exception could be occurred in destructor than you must handled in destructor .Means in destructor you should have catch block to catch that exception. By this way second exception generated is to be handled within a destructor and this exception is not out from the destructor and program is to saved from crash due to two exception generated at a time
Complier handle only one exception at a time. If compiler find more than one exception call abort function by which program terminate abnormally.
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.
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.
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.
Every "module" in an application is responsible for handling its own input parameters. Normally, you should find issues as soon as possible and not hand garbage to another part of the application and rely on them to be correct. There are exceptions though. Sometimes validating an input parameter basically needs reimplementing what the callee is supposed to do (e.g. parsing an integer) in the caller. In this case, it's usually appropriate to try the operation and see if it works or not. Moreover, there are some operations that you can't predict their success without doing them. For example, you can't reliably check if you can write to a file before writing to it: another process might immediately lock the file after your check.
There are no real hard and fast rules around exception handling that I have encountered, however I have a number of general rules of thumb that I like to apply.
Even if some exceptions are handled at the lower layer of your system make sure there is a catch all exception handler at the entry point of your system (e.g. When you implement a new Thread (i.e. Runnable), Servlet, MessasgeDrivenBean, server socket etc). This is often the best place to make the final decision as how your system should continue (log and retry, exit with error, roll back transaction)
Never throw an execption within a finally block, you will lose the original exception and mask the real problem with an unimportant error.
Apart from that it depends on the function that you are implementing. Are you in a loop, should the rest of the items be retried or the whole list aborted?
If you rethrow an exception avoid logging as it will just add noise to your logs.
I generally consider if as the caller of the method I can use an exception in any way (like, to recover from it by taking a different approach) or if it makes no difference and just went wrong if the exception occurs. So in the former case I'll declare the method to throw the exception while in the latter I'll catch it inside of the method and don't bother the caller with it.
The only question on catching exceptions is "are there multiple strategies for getting something done?"
Some functions can meaningfully catch certain exceptions and try alternative strategies in the event of those known exceptions.
All other exceptions will be thrown.
If there are not any alternative strategies, the exception will be simply thrown.
Rarely do you want a function to catch (and silence) exceptions. An exception means that something is wrong. The application -- as a whole -- should be aware of unhandled exceptions. It should at least log them, and perhaps do even more: shut down or perhaps restart.