Error & Exception

Error & Exception

Screen Shot 2016-08-06 at 10.39.14 PM

Error is thrown by JVM in a scenario which is fatal and there is no way for the application program to recover from that error.

  • Unchecked type: Errors happen at run time. They will not be known to compiler
  • Cannot recover
  • eg. java.lang.StackOverflowError, java.lang.OutOfMemoryError

Exception is an event that disrupts the normal flow of the program. It is an object which is thrown at runtime.

  • Unchecked & Checked type: checked exceptions are known to compiler where as unchecked exceptions are not known to compiler because they occur at run time.
  • Can recover from exceptions by handling then through try-catch blocks.

 

Types of exceptions

  • Checked Exception: The classes that extend Throwable class except RuntimeException and Error are known as checked exceptions. Checked Exceptions are checked at compile-time.
    • e.g.IOException, SQLException etc.
  • Unchecked Exception: Unchecked exceptions are not checked at compile-time rather they are checked at runtime.
    • e.g. ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc.

 

Unchecked Exception scenarios

  • ArithmeticException: If we divide any number by zero, there occurs an ArithmeticException.
int a = 50/0;

 

  • NullPointerException: If we have null value in any variable, performing any operation by the variable occurs an NullPointerException.
String s = null;
System.out.println(s.length());

 

  • NumberFormatException: 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.
String s = "abc";
int i = Integer.parseInt(s);

 

  • ArrayIndexOutOfBoundsException: If you are inserting any value in the wrong index, it would result ArrayIndexOutOfBoundsException as shown below.

int a[] = new int[5];

a[10] = 50;

 

Handling checked exceptions

Due to compilation error, checked exceptions need to be handled by programmer. There are two ways to handle these exceptions.

  • Throw when declaring method
  • Try, catch, and finally inside the method

 

Throw when declaring method: Toss the exception handling to user when using the method.

public void connect() throws MyException{
   // try ~ catch ~ finally will not be used
}

 

Try, catch, and finally inside the method: Handling exception inside the method

public void connect(){

   try{
      // Do work here
      System.out.println(10/0);
   } catch (MyException e){
      // Catch lower Exception first
   } catch (Exception e){
      // Highest Exception at the end

      // There are three ways to see the excetption
      e.printStackTrace();
      System.out.println(e.toString());
      System.out.println(e.getMessage());
   } finally {
      // Code on here will be always printed
   }

}
  • e.printStackTrace(): Name of the Exception, Message of the Exception, Stack trace
    • On example code above:
java.lang.ArithmeticException: /by Zero
   at main()
  • e.toString(): Name of the Exception, Message of the Exception
    • On example code above:
java.lang.ArithmeticException: /by zero
  • e.getMessage(): Message of the Exception
    • On example code above:
/by zero

Creating your own Exception

Extends Exception classe: you can make the most suitable exception for your program.

public class MyException extends Exception{
   public Myexception(String msg){
      super(msg);
   }
}

 

 

 

Leave A Comment

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