Rethrowing Exception and User defined exception Rajan Shah Name:
Exception Exception are of two types: Synchronous exceptions- Errors such as “out-of-range index” and “overflow”. Asynchronous exceptions- Errors that are caused by the events beyond the control of the program. Purpose- to provide means to detect and report an “exceptional circumstance” so that appropriate action can be taken.
Error handling code The error handling code performs the following tasks: Find the problem (Hit the exception), Inform that an error has occurred (Throw the exception), Receive the error info (Catch the exception) and Take corrective action (Handle the exception).
Exception handling mechanism Try: The keyword try is used to preface a block of statements which may generate exceptions. Throw: Exception is thrown using throw statement in try block. Catch: Catches the exception thrown by the throw statement in the try block.
Rethrowing an Exception Rethrowing an expression from within an exception handler can be done by calling throw, by itself, with no exception (without arguments). This causes the current exception to be passed on to an outer try/catch sequence. An exception can only be rethrown from within a catch block. When an exception is rethrown, it is propagated outward to the next catch block.
User Defined Exception The example shows the use of std::exception class to implement user defined exception. what() -- is a public method provided by exception class and it has been overridden by all the child exception classes. This returns the cause of an exception.