Types of Exceptions

  • There are two exception types , checked and unchecked (also called runtime).
  • The main difference is that checked exception are checked when compiled.
  • while unchecked exceptions are checked at runtime.
  • As mentioned in our previous lesson , Thread.sleep() throws InterruptedException .
  • This is an example of Checked Exception.
  • Your code will not compile until you have handle the exception.
  • public class Myclass{
           public static void main(String args[]){
                 try{
                        Thread.sleep(1000);
                     }catch(InterruptedException e){
                          .........
                }
         }
    }
  • Example of unchecked Exception , which are checked at runtime .E.g Division by Zero.
  • public class Myclass{
          public static void main(String args[]){
              int a = 7;
              a = a / 10 ;
       }
    }
    Output-
    java.lang.ArithmeticException: / by zero .