编写一个Java程序,在某个方法中对数组下标越界异常、除数为零异常、空指针异常、其他所有异常给出出错时候的对应中文提示,将这些异常继续抛出给main方法,main方法中继续处理该异常,给出main方法中测试四种异常发生时候的输出。
这是最基础的了,自己试着写写吧
其实就是
try
catch()
catch()
catch()
catch()
throw
不同catch里抓不同类型的错误,抓到了就print或者弹窗,之后throw继续往上抛
大致这么个意思
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
public class zz {
/**
* @param args
* @throws ParseException
*/
public static void main(String[] args) throws ParseException {
// TODO Auto-generated method stub
try {
//zz.ArithmeticException();
//zz.NullPointerException();
zz.IndexOutOfBoundsException();
}catch (Exception e) {
System.out.println("main方法中再次处理异常");
}
}
public static void ArithmeticException() throws ArithmeticException{
try {
int a=5/0;
} catch (ArithmeticException e) {
System.out.println("除数为0");
System.out.println("方法运行结束");
throw new ArithmeticException();
}
}
public static void NullPointerException() throws NullPointerException{
try {
List<String> a=null;
a.size();
} catch (NullPointerException e) {
System.out.println("空指针异常");
System.out.println("方法运行结束");
throw new NullPointerException();
}
}
public static void IndexOutOfBoundsException() throws IndexOutOfBoundsException{
try {
List<String> a=new ArrayList<String>();
a.get(0);
} catch (IndexOutOfBoundsException e) {
System.out.println("数组下标越界异常");
System.out.println("方法运行结束");
throw new IndexOutOfBoundsException();
}
}
}