抛出异常
示例代码
public class ExceptionDemo02 {
public static void main(String[] args) throws Exception {
function();
}
public static void function() throws IOException {
FileWriter fw = new FileWriter("a.txt");
}
}
如何处理多个异常
示例代码
package com.ys.exception19;
/*
*
*/
public class ExceptionDemo03 {
public static void main(String[] args) {
try{
// String s = null;
// System.out.println(s.length());
// int[] arr = new int[5];
// System.out.println(arr[8]);
System.out.println(2/0);
}catch(NullPointerException e) {
System.out.println("出现空指针异常");
}catch(ArrayIndexOutOfBoundsException e) {
System.out.println("数组越界异常");
}catch(ArithmeticException e) {
System.out.println("算数异常");
}catch(Exception e) {
System.out.println("异常");
}
}
}
运行结果: