Exception handling in java
11:04:00
In this
tutorial we will discuss what is an exception and how it can be handled in java
programming language.
What is an exception?
An Exception can
be anything which interrupts the normal flow of the program. When an exception
occurs program processing gets terminated and doesn’t continue further. In such
cases we get a system generated error message. The good thing about exceptions
is that they can be handled. We will cover the handling part later in this same
tutorial.
When an exception can occur?
Exception can occur at runtime (known as runtime exceptions) as well as at compile-time (known Compile-time exceptions).
Exception can occur at runtime (known as runtime exceptions) as well as at compile-time (known Compile-time exceptions).
Reasons for Exceptions
There can be several reasons for an exception. For example, following
situations can cause an exception – Opening a non-existing file, Network
connection problem, Operands being manipulated are out of prescribed ranges,
class file missing which was supposed to be loaded and so on.
Difference between error and exception
Errors indicate serious problems and abnormal
conditions that most applications should not try to handle. Error defines
problems that are not expected to be caught under normal circumstances by our
program. For example memory error, hardware error, JVM error etc.
Exceptions are conditions within the code. A developer can handle such conditions and take necessary corrective actions. Few examples –
Exceptions are conditions within the code. A developer can handle such conditions and take necessary corrective actions. Few examples –
- DivideByZero exception
- NullPointerException
- ArithmeticException
- ArrayIndexOutOfBoundsException
Advantages of Exception Handling
- Exception handling allows us to control the normal flow of the program by using exception handling in program.
- It throws an exception whenever a calling method encounters an error providing that the calling method takes care of that error.
- It also gives us the scope of organizing and differentiating between different error types using a separate block of codes. This is done with the help of try-catch blocks.
Why to handle exception?
If an exception is raised, which has not been handled by programmer then
program execution can get terminated and system prints a non user friendly
error message.
Ex:-Take a look
at the below system generated exception
An exception generated by the system is given
below
Exception in thread "main" java.lang.ArithmeticException: / by zero at ExceptionDemo.main(ExceptionDemo.java:5)
ExceptionDemo : The class name
main : The method name
ExceptionDemo.java : The filename
java:5 : Line number
For a novice
user the above message won’t be easy to understand. In order to let them know
that what went wrong we use exception handling in java program. We handle such
conditions and then prints a user friendly warning message to user, which lets
them correct the error as most of the time exception occurs due to bad data
provided by user.
Types of exceptions
There are two
types of exceptions
1)Checked
exceptions
2)Unchecked exceptions
2)Unchecked exceptions
3) Error
Below is a brief
about each however if you want a detailed tutorial with examples then you can
refer Checked and Unchecked exceptions in Java.
Checked exceptions
All exceptions other than Runtime Exceptions are known as Checked exceptions as the compiler checks them during compilation to see whether the programmer has handled them or not. If these exceptions are not handled/declared in the program, it will give compilation error.
All exceptions other than Runtime Exceptions are known as Checked exceptions as the compiler checks them during compilation to see whether the programmer has handled them or not. If these exceptions are not handled/declared in the program, it will give compilation error.
Examples of Checked Exceptions :-
ClassNotFoundException
IllegalAccessException
NoSuchFieldException
EOFException
etc.
Unchecked Exceptions
Runtime Exceptions are also known as Unchecked Exceptions as the compiler do
not check whether the programmer has handled them or not but it’s the duty of
the programmer to handle these exceptions and provide a safe exit.
These exceptions need not be included in any method’s throws list because compiler does not check to see if a method handles or throws these exceptions.
These exceptions need not be included in any method’s throws list because compiler does not check to see if a method handles or throws these exceptions.
Examples of Unchecked Exceptions:-
ArithmeticException
ArrayIndexOutOfBoundsException
NullPointerException
NegativeArraySizeException
etc.
3) Error
Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError,
AssertionError etc.
Common scenarios where exceptions may occur
There are given some scenarios where unchecked exceptions can
occur. They are as follows:
1)
Scenario where ArithmeticException occurs
If we divide any number by zero, there occurs an
ArithmeticException.
. 1. int a=50/0;//ArithmeticException
2)
Scenario where NullPointerException occurs
If we have null value in any variable, performing any operation by
the variable occurs an NullPointerException.
1.
String s=null;
2. System.out.println(s.length());//NullPointerException
3)
Scenario where NumberFormatException occurs
The wrong formatting of any value, may occur
NumberFormatException. Suppose I have a string variable that have characters,
converting this variable into digit will occur NumberFormatException.
1.
String s="abc";
2.
int i=Integer.parseInt(s);//NumberFormatException
4)
Scenario where ArrayIndexOutOfBoundsException occurs
If you are inserting any value in the wrong index, it would result
ArrayIndexOutOfBoundsException as shown below:
1.
int a[]=new int[5];
2. a[10]=50; //ArrayIndexOutOfBoundsException
Java Exception Handling Keywords
There are 5 keywords used in java exception handling.
1.
try
2.
catch
3.
finally
4.
throw
5.
throws
Exception hierarchy
Exception handling in Java
Here are the
list of tutorials which will help you understand the exception handling part in
detail. Read them in the given sequence to grasp the concepts easily.
10. Throws in Java
11. throw vs throws
0 comments
Thanks for intrest.. We will touch withbyou soon..