Wednesday, November 9, 2011

Can constructors throw exception

Tip: Java constructors can throw Exceptions. The throws clause for a constructor is identical in structure and behavior to the clause for a method.

Let's try to put some code:
class Foo {


    Foo () throws FileNotFoundException {

    }

    public static void main(String[] args) {
        try {
            Foo foo = new Foo();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
Above code will compile and run perfectly.

Use SCJP Champ to learn Java The Fun Way!
Available in Android Market

No comments:

Post a Comment