What is an Exception?
NOTE:
Within the try-catch block if there is no chance of rising an exception then we cant write catch block for that exception otherwise we will get compile time error.
Example:
public class Test1
{
public static void main(String[] args) throws Exception
{
try {
System.out.println("Hello");
} catch (IOException e) {
e.printStackTrace();
}
}
}
java: exception java.io.IOException is never thrown in body of corresponding try statement
Default Exception Handler print Exception information in the following format and teminates the program abnormally.
Exception in thread XXX: Name of the Exception: Description
Stack Trace
xxxxxxxxxx
public class Test
{
public static void main(String[] args)
{
doStuff();
}
public static void doStuff()
{
doMoreStuff();
}
public static void doMoreStuff()
{
System.out.println(10/0);
}
}
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Test.doMoreStuff(Test.java:15)
at Test.doStuff(Test.java:10)
at Test.main(Test.java:5)