java.lang.Exception is Object that is thrown when an abnormal event occurs in the flow of program execution,which should be properly handled else causes abnormal termination of the program.
An Exception is an EVENT. it will disturb the normal flow of the program execution.When ever an exception will rised the programmer resposibility to handle and provide grace full termination of the program.
This exception are two types:
1.Checked(Compile time) exceptions
2. Unchecked(Run time(JVM)) Exceptions
We can handle this Exceptions by using Try,catch and finally blocks. Exception handling provides the following advantages over "traditional" error management techniques:
- Separating Error Handling Code from "Regular" Code.
- Propagating Errors up the Call Stack.
- Grouping Error Types and Error Differentiation.
Exception Handler: The Runtime System searches the call stack for a method that contains a block of code that can handle the Exception. This block of code is called an Exception Handler.
Some Important points On Exceptions:
- It is possible to have multiple catch blocks in a try-catch-finally. The finally block is optional.
- A catch block must always be associated with a try block, i.e. can’t have a catch block by itself or with a finally block.
- A finally block must always be associated with a try block, i.e. can’t have a finally block by itself or with a finally block.
- With multiple catch blocks, the type of exception caught must progress from the most specific exception that you wish to catch to the superclasses for these exceptions. (Makes sense!)
- Methods must declare any exception which they throw.
- Invoking a method which declares it throws exceptions is not possible unless either the code is placed in a try-catch, or the calling method declares that it throws the exceptions, i.e. checked exceptions must be caught or rethrown. If the try-catch approach is used, then the try-catch must cope with all of the exceptions which a method declares it throws.
- You can list more than one exception in the throws clause if you separate them with commas.
- RuntimeException and its subclasses are unchecked exceptions.
- Unchecked exceptions do not have to be caught.
- All Errors are unchecked.
- You should never throw an unchecked exception in your own code, even though the code will compile.
- You cannot use a try block on its own. It must be accompanied by a following catch or finally (or both).
- Code in a finally block will always be executed, whether an exception is thrown or not and whether any exception thrown is caught or not. Only terminating the program will stop the finally code from being executed.
- A method can only throw those exceptions listed in its throws clause, or subclasses of those exceptions.
- A method can throw any unchecked exception, even if it is not declared in its throws clause.
- When you override a method, you must list those exceptions that the override code might throw. You can list only those exceptions, or subclasses of those exceptions, that are defined in the method definition you are inheriting from, i.e. you cannot add new exception types to those you inherit. You can choose to throw a subset of those exceptions listed in the method’s superclass, however, a subclass further down the hierarchy cannot then re-list the exceptions dropped above it.
No comments:
Post a Comment