[Java - Coding time]
- Exceptions handling code example. It's always helpful having these code' handy.
[Source Code]
/**
*
*/
package thiago.leoncio;
/**
* @author Thiago
* @Date 07/02/2016
*/
public class ExceptionExample {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
int num1 = 30, num2 = 0;
int output = num1 / num2;
System.out.println("Result = " + output);
} catch (ArithmeticException e) {
System.out.println("[PhoneCoding.com-INFO LOG] Arithmetic Exception: You can't divide an integer by 0");
}
try {
int a[] = new int[10];
// Array has only 10 elements
a[11] = 9;
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("[PhoneCoding.com-INFO LOG] ArrayIndexOutOfBounds");
}
try {
int num = Integer.parseInt("XYZ");
System.out.println(num);
} catch (NumberFormatException e) {
System.out.println("[PhoneCoding.com-INFO LOG] Number format exception occurred");
}
try {
String str = "easysteps2buildwebsite";
System.out.println(str.length());
;
char c = str.charAt(0);
c = str.charAt(40);
System.out.println(c);
} catch (StringIndexOutOfBoundsException e) {
System.out.println("[PhoneCoding.com-INFO LOG] StringIndexOutOfBoundsException!!");
}
try{
String str=null;
System.out.println (str.length());
}catch(NullPointerException e){
System.out.println("[PhoneCoding.com-INFO LOG] NullPointerException..");
}
}
}
Happy, very happy coding,
--Thiago Leoncio