Share this post on:

Exception in java are runtime error which can be handled by the program, the process is called as exception handling. Parent class of exception is Throwable.

Exception : Exception are those runtime error which can be handled by program.

Error : Those runtime error which can’nt handled by the program.

Tools used to handle Exception:

  1. Try
  2. Catch
  3. Finally
  4. Throw
  5. Throws

Try : It is used as try block.try block contains error prone statements. Try block can have multiple catch block but must be in a specific order.

Catch : It is used as catch block. It is an alternative action which occured when exception generate. catch block maintain normal flow of the program.

Finally : Finally keyword is used as finally block. finally block must be exicuted whether an exception occur or not that is finally block must be exicuted to the last wish of developer but it does not maintain normal flow of the program. It must come with try block.

Throw : It is used to rethrowing exception that is from one catch block to another catch block.

Throws : Throws keyword is used when we do not want o use try or catch for temporary that is throws keyword is used for normal flow of class.

Example :

public class Demo {
public static void main(String[] args) {
    int a = Integer.parseInt(args[0]);
    int b = Integer.parseInt(args[1]);
try
{
    System.out.println(a/b);
}
catch(Exception e)
{
    System.out.println(e);
}
System.out.println("Normal Flow");

}}

Example using Finally:

public class Demo {
public static void main(String[] args) {
    int a = Integer.parseInt(args[0]);
    int b = Integer.parseInt(args[1]);
try
{
    System.out.println(a/b);
}
catch(Exception e)
{
    System.out.println(e);
}
finally
{
    System.out.println("Finally");
}
System.out.println("Normal Flow");

}}

There are two types of Exception:

  1. Check Exception : Those exception which must be check or handled at compile time.
  2. Uncheck Exception : Those exception which do not have mandatory to check or handle it at compile time.
Share this post on:
Avatar Raiyan

Author: Raiyan

Hi, I guess you're here because you want to know a bit about me. huh? I am not so good on talking about myself but I'll give a short about me.

My Name is Raiyan. I am a Professional Application Developer and a Blogger.
I started this website to Share my Knowledge. Here I provide all my knowledge whatever I earned till now.

One Comment

  1. Pingback: How to use Custom Exception in Java - ArzaTechs

Leave a Comment

Your email address will not be published. Required fields are marked *