有没有高手帮我看看代码怎么运行不起!
package immoc.com.trycatch; public class Test { /* * 抛出 数组越界和算术异常 */ public void Test1(int x) throws ArrayIndexOutOfBoundsException,ArithmeticException{ System.out.println(x); if(x == 0){ System.out.println("没有异常"); return; } //数据越界异常 else if (x == 1){ int[] a = new int[3]; a[3] = 5; } //算术异常 else if (x == 2){ int i = 0; int j = 5/0; } } }
package immoc.com.trycatch;
public class ExceptionInital {
/**
* @param args
*/
public static void main(String[] args){
//创建对象
ExceptionInital object = new ExceptionInital();
// 调用会抛出异常的方法,用try-catch块
try{
object.Test1(0);
}catch(Exception e){
System.out.println(e);
}
// 数组越界异常
try{
object.Test1(1);
}catch (ArrayIndexOutOfBoundsException e) {
System.out.println("数组越界异常:"+e);
}
// 算术异常
try{
object.Test1(2);
}catch(ArithmeticException e){
System.out.println("算术异常:"+e);
}
//使用 throw 抛出异常(可以抛出异常对象,也可以抛出异常对象的引用)
try{
ArrayIndexOutOfBoundsException exception = new ArrayIndexOutOfBoundsException();
throw exception;//new ArrayIndexOutOfBoundsException();
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("thorw抛出异常:"+e);
}
}