Anda di halaman 1dari 3

EXCEPTION HANDLING Exception handling is provided to catch and handle errors before they occur.

Exception handling is appropriate in situations in which the system can recover from the error causing the exception. Exception is also appropriate for situations in which the program will not be able to recover, but needs to provide orderly cleanup, before shut down. When exception doesnot occur no execution-time overhead is incurred. Exception handling provides another means of returning control from a function or exiting a block of code. The try block directly or indirectly contains the code that may generate an error. In the indirect case, the exception is thrown from a function called directly or indirectly from the try block. When an exception occurs, information may be passed to the exception handler from the point of exception. An exception terminates the block in which the exception has occurred. An exception handler cannot access automatic objects defined within its try block because when an exception occurs the try block terminates and all the automatic objects within the try block are destroyed.

Exceptions should be thrown only within a try block, an exception thrown outside a try block causes call to terminate function. An exception that is not caught causes a call to terminate function.

When the exception handler completes execution, then the execution continues with the first statement after the last catch block. When an exception occurs the try block may not be able to release all the resources before it terminates. The catch handler should release these resources. If an exception handler itself throws an exception, this will be processed by the next outer try block. An exception handler that catches an exception may decide that it cannot process the exception or it may just want to release resources before letting handled by someone else. In this case, the handler rethrows the exception with the statement, throw; Such an exception is handled by the next outer block.

An exception specification lists exceptions that may be thrown by a function, int g ( float h ) throw ( a, b, c ) { // function body } If throw list is empty it means that the function will not throw any exceptions. A function with no exception specification can throw any exception, void g ( ); // this function can throw any exception.

catch( ) will catch all exceptions. Exception classes can be derived from a common base class, this enables uniform handling of related errors. The base-class catch will catch all objects of classes derived from that base class.

Anda mungkin juga menyukai