Exception handling (Source code)
public class abc {
public static void main(String[] args) {
try{
// if there is no Exception
// System.out.println(2/2);
// if there is any exception (anything divide by zero cause to be an infinity answer Java can;t handle the infinity without Exception)
System.out.println(2/0);
}
catch(Exception c){
// to get what is the cause of Exception
System.out.println(c.getLocalizedMessage());
// System.out.println(c.getMessage());
// System.out.println(c.initCause(null));
// to get the exception names
System.out.println(c.toString());
// System.out.println(c.getClass());
}
finally{
System.out.println("I'm the finally part of the Exception handling.\nI must execute whethet there is any exception is detected or not.");
}
}
}
public static void main(String[] args) {
try{
// if there is no Exception
// System.out.println(2/2);
// if there is any exception (anything divide by zero cause to be an infinity answer Java can;t handle the infinity without Exception)
System.out.println(2/0);
}
catch(Exception c){
// to get what is the cause of Exception
System.out.println(c.getLocalizedMessage());
// System.out.println(c.getMessage());
// System.out.println(c.initCause(null));
// to get the exception names
System.out.println(c.toString());
// System.out.println(c.getClass());
}
finally{
System.out.println("I'm the finally part of the Exception handling.\nI must execute whethet there is any exception is detected or not.");
}
}
}
Comments
Post a Comment