WHAT'S NEW?

Exception

Exceptions are the unexpected erroneous events occurring  at run-time breaking the normal flow of code.
There is a difference in Error and Exception

"Error occurs at Compile Time, Exceptions occur at Runtime."

To understand exception in simple term, let us consider this example.

Tom writes a code to load the data from the file let say abc.txt which was kept in D: drive.

His code was working fine.

Then Alex uses the same machine and renames the file to xyz.txt.

Next time Tom tries to run the same code he gets FileNotFoundException.
Java code required the name of file in String it was "abc.txt" but as Alex updated it so Tom's code was unable to find the file and got an Exception and not an Error.

There are many ways to get an exception, some examples are given as follows

  • Trying to fetch data from database table which do not exist
  • Connecting with machine in network with wrong name\
  • Trying to divide a number by zero
  • Attempting to access an invalid array element

Checked and Unchecked Exceptions

Exception have a hierarchy as described below




Exceptions are basically of two types Checked and Unchecked.

Checked Exceptions are those which are sub class of Exception but not the subclass of RuntimeException.
These exceptions need to be handled.

Unchecked Exceptions are those exceptions which are subclass of RuntimeException.
These exceptions need no be handled.

Exception Handling

Exception Handling is a way of making your code run even some unexpected events occur.
There are two ways of handling exceptions
  1. try...catch
  2. throws keyword

try...catch

try...catch block is the best way of handling exceptions

-Any piece of code which is prone to throw exceptions should be kept in try block so that when some untimely event occur it can be handled in catch block

try
{

ANY PIECE OF CODE WHICH IS PRONE TO THROW EXCEPTION GOES HERE
THIS REGION IS ALSO CALLED AS "GUARDED REGION"

}
catch(Exception e)
{
HANDLING CODE GOES HERE
}

try..catch block can be used in 4 ways. Here they are

1.try with single catch

Here we put everything inside a try and we only have a single catch to handle the exceptions occurring.
 try
{
-------
-------
-------
-------
}
catch(Exception e){------}

2.try with multiple catch


Assume we have a code where we have to get the data from database and write it into text file.
So there are chances of two exceptions one which can arise from database and other which can come from IO file. So in this case there is a requirement of multiple catch.

try
{------
Some DB Code
-----
Some IO Code
-----
}
catch(SQLException e)
{
------
}
catch(IOException e)
{
-----
}
catch(Exception e)
{
-----
}

A point to be mentioned here catch(Exception e) should be always kept at last .If generalized exceptions are used earlier you will get a compile time error.


3.try ...catch..finally


finally is a new additional block here. finally is also called as House Keeping Block.
Let say you write a unguarded database code in try block and write exception handling code in catch block. But there cannot be a guarantee that either of the block will get executed to its fullest and all the opened resource can be closed,but the same things can done in finally block.

"May what happen, finally is going to get executed."

try
{
-------
-------
------
}
catch(Exception e)
{
--------
}
finally
{
House Keeping Code
 }


4. try..finally


This can be considered as ducking the exception, as we do nothing to handle the exception , if some exception occurs we are not going to catch it,but just do our house keeping works and finish off.

try
{
------
-------
--------
--------
}
finally
{
-----
}

throws keyword


throws keyword is actually not a type of handling exception but you can duck the exception using this. If a method is going to throw some exception we can use keyword throws for it.

public void m1() throws Exception
{
------------
-------------
----------------
------
}

Now any exception occurring inside this method would be handled by JVM , but the application is going to be terminated.









0 comments:

Post a comment