Exception Handling.

Try Catch ->

// Arithmatic Exception

package Exception_Handling;
public class try_catch {
    public static void main(String[] args) {
        int a=10,b=0,c;
        try{
            c=a/b;
            System.out.println(c);
        }catch(ArithmeticException e){
            System.out.println(e);
        }
    }
}


// NullPointer Exception

package Exception_Handling;
public class try_catch {
    public static void main(String[] args) {
        String s=null;
        try{
            System.out.println(s.toUpperCase());
        }catch(NullPointerException e){
            System.out.println(e);
        }
    }
}

// NumberFormat Exception

package Exception_Handling;
public class try_catch {
    public static void main(String[] args) {
        String s="ved";
        try{
            int a=Integer.parseInt(s);
            System.out.println(a);
        }catch(NumberFormatException e){
            System.out.println(e);
        }
    }
}

// Finally Block


package Exception_Handling;
public class try_catch {
    public static void main(String[] args) {
    try{
    System.out.println("hello");
    int a=10,b=0,c;
    c=a/b;
    System.out.println(c);
    }
    catch(ArithmeticException e){
    System.out.println("can not divide by zero");
    }
    finally{
    System.out.println("finally block");
    }
    System.out.println("main method end");
    }
}
/

// Multiple Try-Catch Blocks


package Exception_Handling;
public class try_catch {
    public static void main(String[] args) {
    try{
    String s="ved";
    int a = Integer.parseInt(s);
    System.out.println(a);
    }
    catch(NumberFormatException e){
    System.out.println("can not change string into number");
    }
    try{
    int[] arr={1,2,3};
    System.out.println(arr[4]);
    }
    catch(ArrayIndexOutOfBoundsException e){
        System.out.println("can not acces 4 index element");
    }
    System.out.println("main method end");
    }
}/j

// Multiple Catch Blocks


package Exception_Handling;
public class try_catch {
    public static void main(String[] args) {
    try{
    String s="ved";
    int a = Integer.parseInt(s);
    System.out.println(a);
     
    int[] arr={1,2,3};
    System.out.println(arr[4]);

    String x=null;
    System.out.println(x.toUpperCase());
    }
    catch(NumberFormatException e){
    System.out.println("number format exception");
    }
    catch(ArrayIndexOutOfBoundsException e){
        System.out.println("array index out of bound exception");
    }
    catch(NullPointerException e){
        System.out.println("null pointer exception");
    }
    catch(Exception e){
        System.out.println("super exception class");
    }
    System.out.println("main method end");
    }
}

// Nested Try Blocks

package Exception_Handling;
public class try_catch {
    public static void main(String[] args) {
    try{
    String s="123";
    int a = Integer.parseInt(s);
    System.out.println(a);
    try{
    int[] arr={1,2,3};
    System.out.println(arr[4]);
    }
    catch(ArrayIndexOutOfBoundsException e){
        System.out.println("array index out of bound exception");
    }
    }
    catch(NumberFormatException e){
    System.out.println("number format exception");
    }
    System.out.println("main method end");
    }
}

// Nested Catch Blocks

package Exception_Handling;
public class try_catch {
    public static void main(String[] args) {
    try{
    int a=10,b=0,c;
    c=a/b;
    System.out.println(c);
    }
    catch(ArithmeticException e){
        System.out.println("not divide by zero");
        try{
            String s="ved";
            int a=Integer.parseInt(s);
            System.out.println(a);
        }
        catch(NumberFormatException a){
        System.out.println("Number format exception");
        }      
    }
    System.out.println("main method end");
    }
}

// Nested Finally Blocks

package Exception_Handling;
public class try_catch {
    public static void main(String[] args) {
    try{
    int a=10,b=2,c;
    c=a/b;
    System.out.println(c);
    }
    catch(ArithmeticException e){
        System.out.println("not divide by zero");  
    }
    finally{
        try{
            String s="ved";
            int a=Integer.parseInt(s);
            System.out.println(a);
        }
        catch(NumberFormatException a){
        System.out.println("Number format exception");
        }    
        finally{
            System.out.println("finally block handle");
        }
        System.out.println("finally block");
    }
    System.out.println("main method end");
    }
}

Comments

Popular posts from this blog

Introduction of java Programming language.

Stack data structure.