exception0
Last updated
Was this helpful?
Last updated
Was this helpful?
java.lang.Throwable class is the superclass of all errors and exceptions in the java language.
java.lang.Exceptions provides for different exceptions thrown under java lang package.
In java, exception is an event that disrupts the normal flow of the program. It is an object which is thrown at runtime.
It is a mechanism to handle runtime errors such as ClassNotFound, IO, SQL, Remote etc.
The core advantage of exception handling is to maintain the normal flow of the application.
The code will continue execute after the exception has been handled.
Checked Exception
Unchecked Exception
Error
1) Checked Exception
The classes that extend Throwable class except RuntimeException and Error are known as checked exceptions e.g.IOException, SQLException etc. Checked exceptions are checked at compile-time.
2)Unchecked Exception
The classes that extend RuntimeException are know as unchecked exceptions e.g. ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. Unchecked exceptions are not checked at compile-time rather they are checked at runtime.
e.g.
Calling an instance method on the object referred by a null reference.
Accessing or modifying an instance field of the object referred by a null reference.
If the reference type is an array type, taking the length of a null reference.
If the reference type is an array type, accessing or modifying the slots of a null reference.
If the reference type is a subtype ofThrowable
, throwing a null reference.
NumberFormatException: A JavaNumberFormatException
usually occurs when you try to do something like convert a String
to a numeric value, like an int, float, double, long, etc.
3)Error
Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.
try
catch
finally
throw
throws
Java try block is used to enclose the code that might throw an exception. It must be used within the method.
Java try block must be followed by either catch or finally block.
Java catch block is used to handle the exception. It must be used after the try block only.
You can use multiple catch block with a single try.
If you have to perform different tasks at the occurrence of different Exceptions, use java multi catch bock.
e.g.
At a time only one Exception is occured and at a time only one catch block is executed.
All catch blocks must be ordered from most specific to most general i.e. catch for ArithmeticException must come before catch for Exception.----Hierarchies
In Java 7, we can catch multiple exceptions using the multi catch syntax:
The try block within a try block is known as nested try block in java.
Sometimes a situation may arise where a part of a block may cause one error and the entire block itself may cause another error. In such cases, exception handlers have to be nested.
Java finally block is a block that is used to execute important code such closing connection, steam etc.
Java finally block is always executed whether exception is handled or not.
Java finally block follows try or catch block.
finally block in java can be used to put "cleanup" code such as closing a file, closing connection etc.
The java throw keyword is used to explicitly throw an exception.
We can throw either checked or unchecked exception in java by throw keyword. The throw keyword is mainly used to throw custom exception.
Syntax:
e.g.1
e.g.2
If a method declares that is throws an exception A, then it is also legal to throw subclasses of A.
NullPointerException: In Java, a specialvalue can be assigned to an object reference.NullPointerException
is thrown when an application attempts to use an object reference that has thevalue.