Exception Handling
Exception :
It is a technique of processing problems that occur during the execution of program.
It is a technique of processing problems that occur during the execution of program.
Difference between Exception and Error :
Exception: It is unwanted and unexpected event that disturb the normal flow of program execution.
Error : Something that goes wrong in our application or program that leads to crash your output or logic.
How Exception Handling Works ?
When a problem occurs exception object is created, thrown and then
handled.
Exception Handling Keyword:
Try- catch
Throw
Throws
Finally
Throw
Throws
Finally
Java Exception Hierarchy:
Types of Exceptions:
Checked Exception : These are the exceptions that the java compiler enforces you to provide either by using try-catch mechanism or throws declaration mechanism. If this requirement is not met ,the compiler will issue an error messages indicating that the exception must be caught or declared.
Various checked exceptions defined in the java.lang.package are,
1. ClassNotFoundException
2. IllegalAccessException
3. InstantiationException
4. NoSuchMethodException
Unchecked Exception: These are the exceptions that the java compiler
doesnot enforces you to explicity provide in the program code.
Various Unchecked Exceptions are,
1. ArithmeticException
2. ArrayIndexOutOfBoundsException
3. ArrayStoreException
4. ClassCastException
5. IllegalArgumentException
6. NegativeArraySizeException
7. NullPointerException
8. NumberFormatException
The java.lang package defines several classes and exceptions.
Exceptions
|
Description
|
Checked
|
Unchecked
|
ArithmeticException
|
Arithmetic Errors such as divide by zero.
|
-
|
Yes
|
ArrayIndexOutOfBoundsException
|
Array index is not within array.length
|
-
|
Yes
|
ClassNotFoundException
|
Related Class not found
|
Yes
|
-
|
IOException
|
Input/Output field not found
|
Yes
|
-
|
IllegalArgumentException
|
Illegal arguments when calling a method
|
-
|
Yes
|
InterruptedException
|
One thred has been interrupted by another thread
|
Yes
|
-
|
NoSuchMethodException
|
Noexistent method
|
Yes
|
-
|
NullPointerException
|
Invalid use of null reference
|
-
|
Yes
|
NumberFormatException
|
Invalid string for conversion to number
|
-
|
Yes
|
Exception Control Flow:
Getting Information From
Exception:
List of Method defined by
Throwable Class.
Methods
|
Description
|
String toString( )
|
Gives you a String object and description of the
exception. This methods is called by the println ( ) method when
an object of throwable is passed to it as argument.
|
String getMessage( )
|
Gives you the description of the exception in
program.
|
Throwable fillInStackTrace( )
|
Gives you aThrowable Object that contains a stack
trace.
|
void print StackTrace( )
|
Gives you and print the stack trace
|
void printStackTrace(PrintStream stream)
|
Return the stack trace to a specific defined
stream
|
String getLocalizedMessage
|
Return the Localized description of the exception
|
Throw: When you want to throw an exception explicitly , then use throw keyword.
Throws: This is used when you are
not using the try catch statement in your code but you know that this
particular class is capable of throwing so and so exception(only checked
exceptions).
Output:
- A new file named test will be created.
Finally: If an exception occur inside a try-catch block and there is no matching catch block, the method terminates without further executing lines of code .
If you want that lines of code must be executed regardless of whether or not there is matching cathch block, put those lines inside the catch block.
Defining Your Own Exceptions:
You can create your own customized exception as per requirements of the application. On each application there is a specific constraints. For this, you create your own customized Exception defining all the constraints and ensure the integrity in the application.