- 异常
- 介绍
- 异常体系图
- 常见的异常
- 运行异常
- 编译异常
- 异常处理
- try-catch-finally
- throws
- 自定义异常
- throw和throws的对比
选中代码块,ctrl+alt+t,选择try-catch
Java语言中,将程序执行中发生的不正常情况称为【异常】
执行过程中所发生的异常事件可分为两类:
- Error(错误):Java虚拟机无法解决的严重问题,如:JVM系统内部错误、资源耗尽等严重情况,比如:StackOverFlowError【栈溢出】和OOM【out of memory】,Error是严重错误,程序会崩溃。
- Exception:其他因编程错误或偶然的外在因素导致的一般性问题,可以使用针对性的代码进行处理。例如空指针访问,试图读取不存在的文件,网络连接中断等等,Exception分为两大类:运行时异常和编译时异常。
- 异常分为两大类:运行时异常和编译时异常
- 运行时异常,编译器不要求强制处置的异常,一般是指编码时的逻辑错误,是程序员应该避免其出现的异常,java.lang.RuntimeException类及它的子类都是运行时异常
- 对于运行时异常,可以不作处理,因为这类异常很普通,若全处理可能会对程序的可读性和运行效率产生影响
- 编译时异常,是编译器要求必须处理的异常
- NUllPointException:空指针异常,当程序需要对象时使用Null
- NumberFormatException:数据格式不正确异常:当应用程序试图将字符串转成一种数值类型,但该字符型不能转换为适当格式
- ArrayIndexOutOfBoundsException:数组下标越界异常:用非法索引访问数组时抛出的异常
- ArithmeticException:数学运算异常,当出现异常的运算条件
- ClassCastException:类型转换异常:当试图将对象强制转换为不是实例的子类时
- SQLException:操作数据库时,查询表可能发生异常
- IOException:操作文件时发生的异常
- FileNotFoundException:当操作一个不存在的文件时,发生异常
- ClassNotFoundException:加载类,而该类不存在时,异常
- EOFException:操作文件,到文件末尾,发生异常
- IllegalArguementException:参数异常
当异常发生时,对异常处理的 方式。
- try-catch-finally 程序员在代码中捕获发生的异常,自行处理,没有finally是允许的
- throws 将发生的异常抛出,交给调用者(方法)来处理,最顶级的处理者就是JVM
- 默认采取throws方法
- 如果异常发生了,则异常发生后面的代码不会执行,直接进入到catch块,如果有finally,最后还需要执行finally里面的语句
- 如果异常没发生,则顺序执行try的代码块,不会进入到catch,如果有finally,最后还需要执行finally里面的语句
- 如果希望不管是否发生异常,都执行某段代码(比如关闭连接,释放资源等),则使用finally{}
- 可以有多个catch语句,捕获不同的异常(进行不同业务处理),要求父类异常在后,子类异常在前,比如(Exception在后,NUllPointException在前),如果发生异常,只能匹配一个catch
- 可以进行try-finally配合使用,这种用法相当于没有捕获异常,因此程序会直接崩掉,finally总是执行
public class Test___ {
public static int method(){
int i = 1;
try {
i++;
String[] names = new String[3];
if(names[1].equals("tom")){
System.out.println(names[1]);
}else{
names[3] = "hello";
}
return 1;
} catch (ArrayIndexOutOfBoundsException e) {
return 2;
} catch(NullPointerException e){
return ++i;//保存到临时变量temp,先不return,等finally执行完毕之后再return,返回临时变量
}finally {
++i;
System.out.println("i=" + i);
}
}
public static void main(String[] args) {
System.out.println(method());
}
}
//输出:i= 4
// 3
throws
如果一个方法(中的语句执行时)可能生成某种异常,但是并不能确定如何处理这种异常,则此方法应显示地声明抛出异常,表明该方法将不对这些异常进行处理,而由该方法的调用者负责处理。
在方法声明中用throws语句可以声明抛出异常的列表,throws后面的异常类型可以是方法中产生的异常类型,也可以是它的父类。
public class Throw01 {
public static void main(String[] args) {
try {
f1();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
//1.FileNotFoundException 编译异常
//2.使用throws抛出异常,让调用f1的调用者(方法)处理
//3.throws后面异常类型可以是方法中产生的异常类型,也可以是它的父类
//4.throws关键字也可以是异常列表,即抛出多个异常
//5.编译异常 调用者必须显式地去处理 try-catch和throws二选一
public static void f1() throws FileNotFoundException {
FileInputStream fis = new FileInputStream("d://aa.txt");
}
}
- 对于编译异常,程序必须处理,比如try-catch或者throws
- 对于运行异常,程序如果没有处理,默认就是throws的方法处理
- 子类重写父类的方法时,对抛出异常的规定:子类重写的方法,所抛出的异常类型要么和父类抛出的异常一致,要么为父类抛出异常的类型的子类型
- 在throws过程中,如果有try-catch,就相当于处理异常,就可以不必throws
class Father{
public void method() throws RuntimeException{
}
}
class Son extends Father{
@Override
public void method() throws NullPointerException{
}
// @Override
// public void method() throws Exception{//扩大异常类型,不允许
// }
}
public class Throw02 {
public static void f1(){//运行异常,默认抛出
f2();
}
public static void f2() throws ArithmeticException{
}
public static void f3() throws FileNotFoundException{//编译异常,显式抛出或处理
f4();
}
public static void f4() throws FileNotFoundException {
}
}
自定义异常
当程序中出现了某些"错误",但该错误信息并没有在Throwable子类中描述处理,这个时候可以自己设计异常类,用于描述该错误信息。
- 定义类:自定义异常类名继承Exception或RuntimeException
- 如果继承Exception,属于编译异常
- 如果继承Exception,属于运行异常(一般继承RuntimeException)
public class CustomException {
public static void main(String[] args) {
int age = 180;
if(!(age >= 18 && age <= 120)){
//通过构造器设置信息
throw new AgeException("年龄需要在18-120之间");
}
System.out.println("年龄正确");
}
}
class AgeException extends RuntimeException{
public AgeException(String message){
super(message);
}
}
//一般自定义异常继承RuntimeException,好处是可以使用默认的处理机制
throw和throws的对比
| 意义 | 位置 | 后面跟的东西 | |
|---|---|---|---|
| throws | 异常处理的一种方式 | 方法声明处 | 异常类型 |
| throw | 手动生成异常对象的关键字 | 方法体中 | 异常对象 |
public class ThrowandThrows {
public static void main(String[] args) {
try {
ReturnExceptionDemo.methodA();
} catch (Exception e) {
System.out.println(e.getMessage());
}
ReturnExceptionDemo.methodB();
}
}
class ReturnExceptionDemo{
static void methodA(){
try {
System.out.println("进入方法A");
throw new RuntimeException("制造异常");
} finally {
System.out.println("A方法的finally");
}
}
static void methodB(){
try {
System.out.println("进入方法B");
return;
} finally {
System.out.println("B方法的finally");
}
}
}
//输入:
//进入方法A
//A方法的finally
//制造异常
//进入方法B
//B方法的finally
public class Homework01 {
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
int num1,num2,res = 0;
while(true){
try {
System.out.println("请输入第一个整数");
num1 = Integer.parseInt(myScanner.next());
break;
} catch (NumberFormatException e) {
System.out.println("您输入的数据格式不正确");
}
}
while(true) {
try {
System.out.println("请输入第二个整数");
num2 = Integer.parseInt(myScanner.next());
res = cal(num1, num2);
System.out.println("res = " + res);
break;
} catch (NumberFormatException e) {
System.out.println("您输入的数据格式不正确");
} catch (ArithmeticException e) {
System.out.println("您输入的除数为0");
}
}
}
public static int cal(int n1,int n2) throws ArithmeticException{
return n1 / n2;
}
}
public class Homework02 {
public static void main(String[] args) {
try {
if(args.length != 2){
throw new ArrayIndexOutOfBoundsException("输入参数个数不对");
}
double res = 0;
res = cal(Integer.parseInt(args[0]),Integer.parseInt(args[1]));
System.out.println("res = " + res);
} catch (ArithmeticException e) {
System.out.println("您输入的除数为0");
} catch (NumberFormatException e) {
System.out.println("您输入的数据格式不正确");
}catch (ArrayIndexOutOfBoundsException e){
System.out.println(e.getMessage());
}
}
public static double cal(int n1,int n2){
return n1 / n2;
}
}
public class Homework04 {
public static void main(String[] args) {
try{
func();
System.out.println("A");
}catch (Exception e){
System.out.println("C");
}
System.out.println("D");
}
public static void func(){
try{
throw new RuntimeException();
}finally {
System.out.println("B");
}
}
}
//输出:
//B
//C
//D
public class Homework05 {
public static void main(String[] args) {
try{
showExce();
System.out.println("A");
}catch (Exception e){
System.out.println("B");
}finally {
System.out.println("C");
}
System.out.println("D");
}
public static void showExce() throws Exception{
throw new Exception();
}
}
//输出:
//B
//C
//D



