exception1
Last updated
Was this helpful?
Last updated
Was this helpful?
An exception is first thrown from the top of the stack and if it is not caught, it drops down the call stack to the previous method, If not caught there, the exception again drops down to the previous method, and so on until they are caught or until they are caught or until they reach the very bottom of the call stack. This is called exception propagation.
In the above example exception occurs in m() method where it is not handled,so it is propagated to previous n() method where it is not handled, again it is propagated to p() method where exception is handled.
Exception can be handled in any method in call stack either in main() method,p() method,n() method or m() method.
RULE: By default, checked exceptions are not forwarded in calling chain(propagated).
The Java throws keyword is used to declare an exception.
If a method needs to be able to throw an exception, it has to declare the exception(s) thrown in the method signature, and then include a throw- statement in the method.
Checked exception only.
reasons:
unchecked Exception: under your control so correct your code
error: beyond your control e.g. you are unable to do anything if there occurs VirtualMachineError or StackOverflowError.
Now Checked Exception can be propagated(forwarded in call stack).
It provides information to the caller of the method about the exception.
output:
exception handled
normal flow...
RULE:
If you are calling a method that declares an exception, you must either caught or declare the exception.
there are two cases:
Case1: You caught the exception i.e. handle the exception using try/catch.
Case2: You declare the exception i.e. specifying throws with the method.
No.
throw
throws
1)
Java throw keyword is used to explicitly throw an exception.
Java throws keyword is used to declare an exception.
2)
Checked exception cannot be propagated using throw only.
Checked exception can be propagated with throws.
3)
Throw is followed by an instance.
Throws is followed by class.
4)
Throw is used within the method.
Throws is used with the method signature.
5)
You can not throw multiple exceptions.
You can declare multiple exceptions e.g. public void method()throws IOException, SQLException.
No.
final
finally
finalize
1)
Final is used to apply restrictions on class, method and variable. Final class can't be inherited, final method can't be overridden and final variable value can't be changed.
Finally is used to place important code, it will be executed whether exception is handled or not.
Finalize is used to perform clean up processing just before object is garbage collected.
2)
Final is a keyword.
Finally is a block.
Finalize is a method.
RULES:
If the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception but it can declare unchecked exception.
If the superclass method declares an exception, subclass overridden method can declare same, subclass exception or no exception but cannot declare parent exception.
Checked exception:
IOException
SQLException
ClassNotFoundException
Unchecked exception:
ArithmeticException
NullPointerException
NumberFormatException
IndexOutOfBoundsException : 1. ArrayIndexOutOfBoundsException 2. StringIndexOutOfBoundsException
Java custom exceptions are used to customize the exception according to user need. By the help of custom exception , you can have your own exception and message.
e.g.
output:
All of these three ways are present in Throwable class. Since Throwable is the base class for all exceptions and errors, so we can use these three methods on any object.
By using this method, we will get name(e.g. java.lang.ArithmeticException) and description(e.g. / by zero) of an exception separated by colon, and stack trace (where in the code, that exception has occurred) in the next line.
e.g.
By using this method, we will only get name and description of an exception. Note that this method is overridden in Throwable class.
e.g.
By using this method, we will only get description of an exception.
e.g.
This text refers to the concept the "call stack" in several places. By the call stack is meant the sequence of method calls from the current method and back to the main Method of the program. If a method A calls B, and B calls C then the call stack looks like this:
When method C returns the call stack only contains A and B. If B then calls the method D, then the call stack looks like this:
In java 7 we can write the code from the example above using the try-with-resource construct like this:
When the try block finishes the FileInputStream will be closed automatically. This is possible because FileInputStream implements the Java interface java.lang.AutoCloseable. All classes implementing this interface can be used inside the try-with-resources construct.
If an exception is thrown both from inside thetry-with-resources
block, and when theFileInputStream
is closed (whenclose()
is called), the exception thrown inside thetry
block is thrown to the outside world. The exception thrown when theFileInputStream
was closed is suppressed. This is opposite of what happens in the example first in this text, using the old style exception handling (closing the resources in thefinally
block).
This example creates two resources inside the parentheses after thetry
keyword. AnFileInputStream
and aBufferedInputStream
. Both of these resources will be closed automatically when execution leaves thetry
block.
The resources will be closed in reverse order of the order in which they are created / listed inside the parentheses. First theBufferedInputStream
will be closed, then theFileInputStream
.
The try-with-resources construct does not just work with Java's built-in classes. You can also implement the java.lang.AutoClosable interface in your own classes, and use them with the try-with-resources construct.
The AutoClosable interface only has a single method called close().
output: