运行时异常(RuntimeException)
定义:运行时异常,即程序运行时抛出的异常。这种异常在写代码时不进行处理,Java源文件也能编译通过。 RuntimeException异常类及其下面的子类均为运行时异常。
注意;运行时异常会直接或间接继承RuntimeException,通常不需要显式throws异常类处理
package com.jd.test2;
//自定义异常类
public class AgeException extends RuntimeException{
public AgeException (String msg) {
super(msg);
};
}
package com.jd.test1;
//这里自定义了AgeException类继承运行时异常类
import com.jd.test2.AgeException;
//运行时异常(RuntimeException)
public class Test1 {
//运行时异常可以直接throw抛出异常,即打印异常提示信息,不需要显式throws处理,但此时抛出代码后,下面代码不会运行
//throw异常抛出一般写在方法体中或代码块中
//也可以自己解决,使用try-catch-finally方法,解决之后会打印红字异常提示信息,但是和throw相比,下面代码会继续运行
private int age;
public void setAge(int age){
if(age<0 || age>150) {
throw new AgeException("年龄大于零岁小于150岁");
}else {
System.out.println(age);
}
}
public static void main(String[] args) {
Test1 test1 = new Test1();
test1.setAge(160);
System.out.println("晚安");
}
}
package com.jd.test1;
//这里自定义了AgeException类继承运行时异常类
import com.jd.test2.AgeException;
//运行时异常(RuntimeException)
public class Test1 {
//运行时异常可以直接throw抛出异常,即打印异常提示信息,不需要显式throws处理,但此时抛出代码后,下面代码不会运行
//throw异常抛出一般写在方法体中或代码块中
//也可以自己解决,使用try-catch-finally方法,解决之后会打印红字异常提示信息,但是和throw相比,下面代码会继续运行
private int age;
public void setAge(int age){
if(age<0 || age>150) {
throw new AgeException("年龄大于零岁小于150岁");
}else {
System.out.println(age);
}
}
public static void main(String[] args) {
Test1 test1 = new Test1();
try {
test1.setAge(160);
} catch (AgeException e) {
e.printStackTrace();
}
System.out.println("晚安");
}
}
检查时异常(CheckedException)
定义:检查时异常,又称为非运行时异常,这样的异常必须在编程时进行处理,否则就会编译不通过。Exception异常类及其子类(除去RuntimeException异常类及其子类)都是检查时异常。
package com.jd.test2;
//这里自定义AgeException为检查时异常类
public class Test2 {
//检查时异常不能直接throw抛出异常对象,需要显式处理
private int age;
public void setAge(int age) throws AgeException {//这里异常抛给方法调用者
if(age<0 || age>150) {
throw new AgeException("年龄大于零岁小于150岁");//这里异常隐式向上抛
}else {
System.out.println(age);
}
}
public static void main(String[] args) throws AgeException {//这里异常抛给JVM
Test2 test2 = new Test2();
test2.setAge(160);
System.out.println("晚安");
}
}
注意:异常抛出后线面代码不会执行
package com.jd.test2;
//这里自定义AgeException为检查时异常类
public class Test2 {
//检查时异常不能直接throw抛出异常对象,需要显式处理
//也可以使用try-catch-finally方法处理
private int age;
public void setAge(int age){
if(age<0 || age>150) {
//try-catch-finally方法处理,异常被打印出类,下面代码继续运行
try {
throw new AgeException("年龄大于零岁小于150岁");
} catch (AgeException e) {
e.printStackTrace();
}
}else {
System.out.println(age);
}
}
public static void main(String[] args) throws AgeException {
Test2 test2 = new Test2();
test2.setAge(160);
System.out.println("晚安");
}
}
package com.jd.test2;
//这里自定义AgeException为检查时异常类
public class Test2 {
//检查时异常不能直接throw抛出异常对象,需要显式处理
//也可以使用try-catch-finally方法处理
private int age;
public void setAge(int age) throws AgeException{
if(age<0 || age>150) {
throw new AgeException("年龄大于零岁小于150岁");
}else {
System.out.println(age);
}
}
public static void main(String[] args){
Test2 test2 = new Test2();
try {
test2.setAge(160);
} catch (AgeException e) {
e.printStackTrace();
}
System.out.println("晚安");
}
}
两种方法都可以解决问题
总结:
如果是运行时异常:运行时异常可以直接throw抛出异常,即打印异常提示信息,不需要显式throws处理,但此时抛出代码后,下面代码不会运行;
也可以自己解决,使用try-catch-finally方法,解决之后会打印红字异常提示信息,但是和throw相比,下面代码会继续运行
如果是检查时异常:检查时异常不能直接throw抛出异常对象,需要显式throws异常类处理,也可以使用try-catch-finally方法处理。同样的,throw抛出异常后代码停止,try-catch-finally解决后代码继续运行。