Throwable类(超类):所有异常和错误的超类
Error(子类):错误
Exception(子类):编译期异常
RuntimeException(Exception的子类):运行期异常
异常处理的五个关键字try、catch、finally、throw、throws
作用:可以在指定的方法中抛出指定的异常
格式:throw new XXXException(“产生的原因”)
注意:
1、throw关键字必须写在方法的内部
2、throw关键字后边new的对象必须是exception或者exception的子类对象
3、throw关键字指定的异常对象,我们就必须处理这个异常对象
throw关键字后边创建的是runtimeexception或者是其子类对象,我们可以不处理,交给JVM处理
4、throw关键字后边创建的异常是编译异常(写代码时出错),我们就必须处理这个异常,要么throws,要么try……catch
public class Exception1 {
public static void main(String[] args) {
int[] arr = {1,2,3};
int element = getElement(arr,7);
System.out.println(element);
}
public static int getElement(int[] arr,int index){
/*
对传递的参数进行合法性检查
*/
if(arr == null){
throw new NullPointerException("传递的数组的值为空");
}
if (index < 0 || index > arr.length - 1){
throw new ArrayIndexOutOfBoundsException("索引有问题");
}
int element = arr[index];
return element;
}
}
Objects类里面的非空判断
Objects.requireNonNull(obj,String)
if(obj == null){
throw new NullPointerException(String);
}
return obj;
作业:当方法内部抛出异常对象的时候,那么我们就必须处理这个异常对象
可以使用throws关键字处理异常对象,会把异常对象声明抛出给方法的调用者处理,最后抛给JVM处理。
格式:在方法声明时使用
修饰符 返回值类型 方法名(参数列表) throws Exception1,exception2{
throw new exception1();
throw new exception2();
}
try……catch
格式:
try
{可能产生异常的代码}
catch(定义一个异常变量,接受try产生的异常)
{异常的处理逻辑(产生异常后,怎么处理异常)}
catch(异常类名 变量名)
{}
String getMessage() :返回此Throwable的简短描述
String toString() :返回Throwable的详细消息字符串
void printStackTrace() : JVM打印异常对象
无论是否出现异常,都会执行
注意:
1、finally代码块,不能单独使用,必须和try一起使用
2、finally一般用于资源释放(资源回收),无论程序是否出现异常,都会执行
1、多个异常分别处理
import java.util.List;
public class Exception2 {
public static void main(String[] args) {
try{
int[] arr = {1,2,3};
System.out.println(arr[3]);
}catch (ArrayIndexOutOfBoundsException e){
System.out.println(e);
}
try {
List<Integer> list = List.of(1,2,3);
System.out.println(list.get(3));
}catch (IndexOutOfBoundsException e){
System.out.println(e);
}
}
}
2、多个异常一次捕获,多次处理
catch里面定义的异常变量,如果有继承关系,那么子类的异常变量必须写在上边,否则出错。
import java.util.List;
public class Exception2 {
public static void main(String[] args) {
try{
int[] arr = {1,2,3};
System.out.println(arr[3]);
List<Integer> list = List.of(1,2,3);
System.out.println(list.get(3));
}catch (ArrayIndexOutOfBoundsException e){
System.out.println(e);
}catch (IndexOutOfBoundsException e){
System.out.println(e);
}
}
}
3、一次捕获,一次处理
public class Exception2 {
public static void main(String[] args) {
try{
int[] arr = {1,2,3};
System.out.println(arr[3]);
List<Integer> list = List.of(1,2,3);
System.out.println(list.get(3));
} catch (IndexOutOfBoundsException e){
System.out.println(e);
}
}
}
1、如果父类抛出了多个异常,子类重写父类方法时,抛出和父类相同的异常或者是父类异常的子类或者不抛出异常
2、父类方法没有抛出异常,子类重写父类该方法时不能抛出异常,此时子类产生该异常,只能捕获处理,不能声明抛出。
public class Fu {
public void method1() throws NullPointerException,ClassCastException{}
public void method2() throws IndexOutOfBoundsException{}
public void method3() throws IndexOutOfBoundsException{}
public void method4(){}
}
class Zi{
public void method1() throws NullPointerException,ClassCastException{}
public void method2() throws ArrayIndexOutOfBoundsException{}
public void method3() {}
public void method4(){}
}
/*
格式:
添加一般空参数的构造方法
添加一个带异常信息的构造方法
*/
public class CustomException extends Exception/* RuntimeException*/{
/*
1、自定义异常类一般以Exception结尾,说明这是一个异常类
2、自定义异常类,必须继承Exception/RuntimeException
继承Exception,那么自定义的异常就是一个编译期异常,
如果方法内部抛出了编译期异常,就必须处理这个异常,要么throws要么try……catch
继承RuntimeException那么自定义的异常就是一个运行期异常,无需处理,交给JVM处理
*/
public CustomException(){ super(); }
public CustomException(String message){ super(message);}//传递错误信息
}
原文:https://www.cnblogs.com/saonian450/p/12549560.html