异常
main方法接受到了这个异常,main方法也没有异常的处理逻辑,继续抛出给main调用者JVM处理,jVM打印出这个异常,并且终止当前执行的这个程序。
一、异常的处理
throw关键字:可以使用此关键字在指定的方法中抛出指定的异常。
注意:
1.throw必须写在方法的内部。
2.new 的对象必须是Exception对象或者其子类。
3抛出的异常对象后,必须处理这个异常对象,其抛出的是编译期异常,如果是RuntimeException或者其子类异常,可以不用处理。
throw new XXXexception("异常产生的原因");
public class DemoThrow{
//首先要对传递给方法的参数进行合法性校验
public static int getElement(int[] arr,int index){
if(arr==null){
throw new NullPointerException("传递数组值是空");//是运行期间异常
}
if(index<0||index>arr.length-1){
throw new ArrayIndexOutOfBoundsException("数组索引越界");//是运行期间异常
}
}
}
二、Objects非空判断
源码
public static <T>T requireNonNull(T obj){
if(obj==null) throw New NullPointerException();
return obj;
}
使用
Object obj = new Object();
Objects.requireNonNull(obj);
Objects.requireNonNull(obj,"自己描述异常的信息")
三、throws声明异常的方式
throws :
异常处理的第一种方法,交给上一级处理。
作用:
会把异常对象声明抛出给方法的调用者处理。
使用格式:
修饰符 返回值类型 方法名(参数列表) throws AAAException,BBBBException{
throw new AAAException("异常信息描述");
throw new BBBBException("异常信息描述");
}
注意:
1.方法内部抛出了多个异常对象,如果这些异常对象有子父类关系,直接声明父类即可。
2.throw必须写在方法声明处。
3.throws声明的异常必须是Exception或者其子类对象。
4调用一个声明抛出异常的方法,就必须在此方法处理(try…catch),要么接着抛出到上一级。
四、try-catch
try...catch:异常处理的第二种方式,自己处理异常
格式:
try{
可能产生异常的代码。
}catch(定义一个异常变量,用来接收try中抛出的异常){
异常处理逻辑,一般会把异常信息记录到一个日志文件中。
}
注意:
1.try中可能会抛出多个异常对象,那么可就使用多个catch来处理这些异常对象。
2.如果try中产生了异常,那么就会执行catch中的异常处理逻辑,执行完毕之后,继续执行之后的代码,如果try中没有产生异常,那么跳
过catch中的代码。
public class TryCatchDemo {
public static void main(String[] args) {
try {
readFile("d:/a.tx");
}catch (IOException e){
System.out.println("文件类型错误");
System.out.println(e);
}
System.out.println("后续代码");
}
public static void readFile(String fileName) throws IOException{
if(!fileName.endsWith(".txt")){
throw new IOException("文件类型不匹配");
}
}
}
五、Throwable类中的3个异常处理方法
1.String getMessage() 返回此Throwable 的简要信息
2.String toString() 返回此Throwable的详细消息字符串
3.void printStackTrace() jvm默认调用此方法,异常信息最完善
六、finally代码块
程序必须执行的代码写在finally中。
import java.io.IOException;
public class FinallyStatemtn {
//①finally不能单独使用过,配合try②一般用于资源释放,程序是否出现异常,资源必须释放
public static void main(String[] args) {
try {
readFile("d:/a.t");
} catch (IOException e) {
e.printStackTrace();
}finally {
System.out.println("资源释放");
}
}
public static void readFile(String fileName) throws IOException {
if(!fileName.endsWith(".txt")){
throw new IOException("文件类型不匹配");
}
}
}
七、异常处理注意事项
1.多个异常处理:
1.1多个异常一次捕获,分别处理。
catch里面定义的异常变量,如果有父子类关系,则***子类的异常变量必须写在上面***,否则会报错
public class ManyException {
public static void main(String[] args) {
int []arr ={1,2,3};
List<Integer> list = new ArrayList<>() ;
list.add(1);
list.add(2);
list.add(3);
try {
System.out.println(arr[3]);
System.out.println(list.get(6));
}catch (ArrayIndexOutOfBoundsException a){
System.out.println(a+"数组越界");
}catch (IndexOutOfBoundsException e){
System.out.println(e+"索引越界");
}
}
}
1.2分别捕获分别处理
public class ManyException {
public static void main(String[] args) {
int []arr ={1,2,3};
List<Integer> list = new ArrayList<>() ;
list.add(1);
list.add(2);
list.add(3);
try {
System.out.println(arr[3]);
}catch (ArrayIndexOutOfBoundsException a){
System.out.println(a+"数组越界");
}
try {
System.out.println(list.get(6));
}catch (IndexOutOfBoundsException e){
System.out.println(e+"索引越界");
}
}
}
1.3多个异常一次捕获一次处理、
public class ManyException {
public static void main(String[] args) {
int []arr ={1,2,3};
List<Integer> list = new ArrayList<>() ;
list.add(1);
list.add(2);
list.add(3);
try {
System.out.println(arr[3]);
System.out.println(list.get(6));
}catch (Exception e){
System.out.println(e);
}
}
}
运行时异常可以不用处理
2.如果finally有return 语句,永远返回finally中的结果,避免该情况。
3.子父类异常
3.1如果父类抛出了多个异常,子类重写父类方法时,抛出和父类相同的异常或者抛出父类异常的子类或者不抛出异常。
3.1父类方法没有异常,子类重写也不能抛出异常。
3.3子类产生异常,智能catch不能抛出。