Exception Handling
Exception Handling
syntax : try{
-------------
}catch(){
--------------
}
1) Arithmetic Exception.
Example 1: No Exception
package basicsOfException;
Example 2: Exception
package basicsOfException;
Example 3:
package basicsOfException;
public class DiffrentExceptions {
Example 1:
package basicsOfException;
String s = "Santosh";
System.out.println(s.length());
System.out.println("Main method Ended");
}
}
Example 2:
package basicsOfException;
String s = null;
System.out.println(s.length());
System.out.println("Main method Ended");
}
}
Example 3:
package basicsOfException;
String s = null;
try {
System.out.println(s.length());
}catch(NullPointerException n) {
System.out.println("Catch Block");
}
System.out.println("Main method Ended");
}
}
3) NumberFormat Exception.
Example 1:
package basicsOfException;
String s = "123";
Example 2:
package basicsOfException;
String s = "123Santosh";
Example 3:
package basicsOfException;
String s = "123Santosh";
try {
int parseInt = Integer.parseInt(s);
System.out.println(parseInt);
} catch (NumberFormatException n) {
System.out.println("Catch Block");
}