A place for Extreme Java JEE developers to read latest Java tutorials. The Extreme Java blog is for Java application development, J2EE application development and web technologies in the Java world.
Apr 3, 2012
Throwing Exception from main method in Java
1) The main method present in a class can throw an exception.
2) If an uncaught exception is thrown from main method then the Java program will exit.
3) One need to specify the checked exceptions with main method using throws keyword.
4) The following code shows how exceptions work in main method:
public class Test{
public static void main(String[] args) throws IOException {
File f = new File("c://test");
FileReader fr = new FileReader(f);
System.out.println(f.read());
}
}
5) The JVM will exit with an exception.
6) You will get compiler error for invoking any method which throws checked exception and specifies it.
7) In case of exception, the JVM will exit with printing the stack trace.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment