www backpage com detroit mi backpage escorts palm springs tranny escorts philly transex panama city fl denver backpages

[EXPLAINED] Java Exceptions

In simple words an Exception is a runtime error, that does not follow that a particular rule and disturbs the normal flow of a program. It is object that occur at runtime that occurs within a method and contains: Information about the error including its type. In real life it simply a situation for which we are not ready or we are not expecting this situation which disturb our routine life. Therefore, Java programmers must handle the exceptions in to maintain the operation and normal flow of a program and reduce common javascript errors from occurring.

There are three types of exception according to Oracle :

  1. Checked
  2. Unchecked
  3. Error

Checked:

IOException ,SQLException etc are checked exception which are check at compile time.

Unchecked:

The exception which are checked at runtime are called unchecked exception are ArithmeticException, NullPointerException,ArrayIndexOutOfBoundsException

Error

Error which are difficult to handle are OutOfMemoryError, AssertionError etc.

 

Keywords for JAVA Exception

Try:

It specify a block where we place exception code simply for which we have doubt that exception can occur and disturb the normal flow of code.

catch:

This is the block where we handle the exception.It is must be use after try keywords,it means we cant use catch alone.

finally

In this block we specify that code which must be execute either exception handled or not.

Throw

It is does not throw exception but used to declare exception.

Let’s understand exception and exception handling with example.

See in that example how exception disturb the flow program.

package javaException;
public class exception {
public static void main(String[] args)
{
System.out.println("1");
System.out.println("2");
System.out.println("3");
System.out.println("4");
System.out.println(100/0);
System.out.println("5");
System.out.println("6");
System.out.println("7");
System.out.println("8");
}
}

Output:

We can see Exception is only at line No 11 so, we assume that only the line which have exception will not execute and the rest of code will execute.

But we can clearly see in output, statements above the exception line executed but after the exception rest of statements didn’t executed because the exception disturb the flow of program and didn’t execute the rest of statements after the exception.

So,for that situation we must handle the exception.

Let’s see in example

package javaException;
public class exception {
public static void main(String[] args)
{
System.out.println("1");
System.out.println("2");
System.out.println("3");
System.out.println("4");
try {
System.out.println(100/0);//Exception occur
}
catch(ArithmeticException e)//exception handling
{
System.out.println(e);
}
System.out.println("5");
System.out.println("6");
System.out.println("7");
System.out.println("8");
}
}

Here we handle the exception using try-catch.

Let see output

Solutions-for-Stack-OverFlow-Error-infinite-recursion-java

So we can clearly see that by handle the exception 100/0 which raises the ArithmeticException handled by try-catch and our normal flow of code didn’t disturb and after the exception the rest of statements executed.

 

Comments are closed.