Java Exception Handling. If you want to get knowledge and clear understanding about use of exception handling in Java. Then its a best place for you, here i will teach you in detail about exception handling with very easy to understand examples.
Java Exception Handling
The
most vital and usable feature in Java programming is the Exception handling
because with the help of this, programmer can handle those errors which usually
occur at run time. Sometimes, in programming, we may get some errors because of
which the normal execution of the program may get interrupted i.e. the flow of
the program get disturbed and it causes very bad system generated messages.
These error messages are sometimes very technical and not easy to understand.
But
because of the exception handling, we can handle this situation and we can
write our own messages which are easy to understand by the normal users if in
case exception occurs. We may have different types of exceptions in Java and to
handle them, we have different classes already provided by the Java language.
Reasons for Exceptions in Program
Exception
in programs occur because of many different reasons. When exception occurs, it
doesn’t mean that our code is wrong. Code will be fine and we will not have any
compilation errors but still we can face exception and reasons can be that we
are making a network related program so we can get network exception. We have
an array, and we are accessing an element which is not present in the array. We
try to open a file which is deleted already or we are getting wring data from
users.
Checked Exception in Java
This
is the exception which is checked during compile time. In case of checked
exception, method should take care of handling them with the help of try and
catch block
Unchecked Exception in Java
These
are the exceptions which are not checked during compilation time. And if these
are not handled still we will not get error of compilation. These usually occur
because of wrong data provided by user
Main reason to use the Exception Handling
When
we have exception in our program, our user who is using our program may get
very technical errors. These errors are not in easy language because these are
system generated and for a normal user, they are very difficult to understand. So
to show them a proper simple message about error, we can use exception handling
and provide our own simple message to users so that they can have an idea about
what was wrong.
For
Example
ArrayOutOfBoundException
This
exception occurs when we have an array of let’s suppose 5 elements and we are
trying to access 6th element in that array and as it is not
available so system will through this exception. So we as a programmer have to
use exception handling to provide a simple message though which user can easily
understand the problem.
In
Java, we have different keywords for exception handling. These are
1.
Try
2.
Catch
3.
Finally
4.
Throw
5.
Throws
This is the block of code about which user
can think that exception can occur. So we can place the code in try block. It
will take care of any exception that can be occur in code
2.
Catch block
When there is any
exception occurs in the code, it is forwarded to a block known as catch block.
Error can be catch using this catch block
3.
Throw keyword is used
when we want to throw an exception manually
4.
Throws is used to show
that a method can throw an exception
5.
Finally block is used
when we want to execute a code in any case after try block
Syntax
try
{
//code
}
catch
(Exception e)
{
//code
}
finally
{
//code
}
We
can use more try blocks if we have chance of more than one exception. We have
to use separate try blocks for every statement that might throw an exception.
Every try block, we can have more than one catch block or can be zero catch
block. As there are different types of exceptions, so we have to use different
classes to handle them in catch block in the argument. Finally block is
optional and it will be executed if exception occurs or not
Example
Program 1
public
class ExceptionExample
{
public static void main (String ar[ ])
{
try
{
int a,b;
a = 5;
b =
0;
int ans
= a/b;
}
catch
(ArithmeticException obj)
{
System.out.println(obj);
}
}
}
Output
will be:
Exception
in thread main java.lang.ArithmeticException: / by zero
We
may have other exceptions in our program. For Example
String
a = null;
System.out.println(a.length);
This will
generate null pointer exception here
String
b = “hello”;
System.out.println(Integer.parseInt(b);
This will
generate number format exception here
int x[
] = new int[3];
x[20]=100;
This
will create array index out of bound exception here
Example
Program 2
public
class ExceptionTest
{
public static void main (String ar [])
{
try
{
int a[
] = {1,2,3};
System.out.println(arr[5]); // exception can
occur
}
// to handle it, we can use
catch block here
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
}
}
}
Example
Program 3
public
class ExceptionExampleWithFinally
{
public
static void main (String[] ar)
{
try
{
int[] a = new int[4];
a[10] = 100;
System.out.println("exception");
}
catch(ArrayIndexOutOfBoundsException
ex)
{
System.out.println("Exception
handling");
}
finally
{
System.out.println("some
code here in finally block");
}
System.out.println("Outside
code here");
}
}
Finally
block code will be executed either exception occurs or not.
Example
Program 4
public
class ExceptionExp
{
public
static void main (String[] args)
{
int[]
a = new int[4];
try
{
int x = a[4];
//we
will have exception here because of array index but in the catch block we are
using different class of exception to handle array exception which is actually
wrong and that’s why it will not be handled in the catch block
System.out.println("something
here");
}
catch(NullPointerException
ex) // this is not relevant class of exception to handle array exception
{
System.out.println("Exception
handling");
}
finally
{
System.out.println("finally
block here");
}
}
}
Output
will be:
Final
block here
Exception
in thread "main" java.lang.ArrayIndexOutOfBoundsException:
// This
is because we are not using the proper class to handle Array exception
No comments:
Post a Comment