Home » Java programming language

Examples on Exceptional handing in Java

Here, we have some of the examples on Exceptional Handling in java to better understand the concept of exceptional handling.
Submitted by Saranjay Kumar, on March 09, 2020

Here, we will analyse some exception handling codes, to better understand the concepts.

Try to find the errors in the following code, if any

Code 1:

public class prog {
    public static void main(String arg[]) {
        try {
            int a = 10, b = 0;
            int c = a / b;
        } catch (RuntimeException e) {
            System.out.println(e.getMessage());
        } catch (ArithmeticException e) {
            System.out.println(e.getMessage());
        }
    }

}

Output

/prog.java:8: error: exception ArithmeticException has already been caught
        } catch (ArithmeticException e) {
          ^
1 error

Explanation:

When using multiple catch blocks, we have to make sure that the exceptions are caught in the reverse order of inheritance of the exceptions. This means that most specific exceptions must be caught before the most general exceptions. ArithmeticException must be caught before the RuntimeException.


Code 2:

public class prog {
    public static void main(String arg[]) {
        try {
            throw new Integer(25);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

Output

/prog.java:4: error: incompatible types: Integer cannot be converted to Throwable
            throw new Integer(25);
            ^
Note: /prog.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
1 error

Explanation:

We can only throw objects of Throwable class or classes that inherit this class. Integer class does not extend the Throwable class and thus, we cannot throw objects of Integer class. Similarly the statement "throw 25" is erroneous. Unlike C++, where we can throw objects, in Java we can only throw those objects that inherit the Throwable class.


Code 3:

public class prog {
    public static void main(String arg[]) {
        err ob1 = new err();
        ob1.func();
    }
}

class err {
    void func() {
        try {
            System.out.println("Inside try");
        } finally {
            System.out.println("Inside finally");
        }
    }
}

Output

Inside try
Inside finally

Explanation:

It is not necessary that we attach a catch block to a try block. Try statement can be associated with a finally statement directly.


Code 4:

import java.io.*;

public class prog {
    public static void main(String arg[]) {
        err ob1 = new err();
        ob1.func();
    }
}

class err {
    void func() throws IOException {
        try {
            System.out.println("Inside try");
        } catch (Exception e) {
            System.out.println("Inside catch");
        } finally {
            System.out.println("Inside finally");
        }
    }
}

Output

/prog.java:6: error: unreported exception IOException; must be caught or declared to be thrown
        ob1.func();
                ^
1 error

Explanation:

If a function is said to throw any checked exception, then that checked exception must be caught by the caller. The statement 'void func() throws IOException' clearly states that the function can throw an IOException that must be handled by the caller. The error can be corrected by enclosing the 'ob1.func()' statement in a try-catch block.


Code 5:

import java.io.*;

public class prog {
    public static void main(String arg[]) {
        err ob1 = new err();
        ob1.func();
    }
}

class err {
    void func() throws RuntimeException {
        try {
            System.out.println("Inside try");
            try {
                int[] a = new int[10];
                int c = 10;
                a[c] = 0;
            }
        } catch (Exception e) {
            System.out.println("Inside catch");
        }

    }
}

Output

/prog.java:14: error: 'try' without 'catch', 'finally' or resource declarations
            try {
            ^
1 error

Explanation:

Every try block must have an associated catch or finally block. In the code, the inner try block does not have an associated catch or finally block.








Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.