Exception in java

An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions. Exceptions are two types , one is runtime exception (occurs at runtime) and another one is compile time (occurs in compile time) .

                             


 Runtime exceptions are  as follows :
  •   DivideByZeroException
  •   NullPointerException
  •   ArithmeticException
  •   ArrayIndexOutOfBoundsException 
  •   NumberFormatException etc
Compile time exception is
  •     Syntax error 

Example of NullPointerException :

public class HelloWorld
{
  public static void main(String[] args)
  {
    String str=null;
    System.out.println("String length : "+ str.length());
  }
}

 Result:
              Exception in thread "main" java.lang.NullPointerException
              at HelloWorld.main(HelloWorld.java:6)

Example of DivideByZeroException :

 public class HelloWorld
  {
    public static void main(String[] args)
     {
         System.out.println("Devide by Zero : "+ (10/0));
     }
  }

 
  Result: 
              Exception in thread "main" java.lang.ArithmeticException: / by zero
              at HelloWorld.main(HelloWorld.java:6)


Example of Compile time exception :

 public class HelloWorld
  {
    public static void main(String[] args)
     {
         System.out.println("Devide by Zero : "+ (10/0))
     }
  }


 Result:
           error: ';' expected
           System.out.println("Devide by Zero : "+ (10/0))
                                                   ^
          1 error


Find us :
        Facebook : @apnaandroid
        Google+   : Apna Java
        Youtube : Android & Java Tutorial

Comments

Popular posts from this blog

Disable/Hide Year from DatePickerDialog in android

Custom Calendar in android

Constructor in Java