java的异常处理

1.异常

  异常是指在程序的运行过程中所发生的不正常的情况,它会中断正在运行的程序。

在java中,通过jvm的异常处理机制为程序提供的处理异常的能力,从而保证了程序能继续运行的可能。

1.1 异常处理

  涉及异常处理的关键字有try…catch/try…catch…finally

  1.1.1 try/catch

    可能产生异常的代码放到try代码块中,catch代码块负责捕获处理异常。

    (1)程序正常执行,未出现任何异常

    (2)出现异常,进行异常处理,正常结束

    Exception是所有异常类的直接或间接父类。

package cn.sxt02.exception02;
import java.util.Scanner;
public class Test01 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入第一个数:");

		int num1 = 0;
		int num2 = 0;
		
		try {
			num1 = sc.nextInt();

			System.out.println("请输入第二个数:");
			num2 = sc.nextInt();

			int r = num1 / num2;
			System.out.println("num1/num2 = " + r);
		}catch (Exception e) {
			System.out.println("程序出现异常");
			// 打印异常的信息
			// System.out.println(e.toString());
			
			
			// 打印异常堆栈信息
			 e.printStackTrace();
			
			// 返回异常的描述信息,如果没有信息,返回null(InputMismatchException 没有描述信息)
			System.out.println(e.getMessage());
		}
		
		System.out.println("程序正常结束");
	}
} 

    

常见的异常:

    printStackTrace:打印异常的执行堆栈信息

  一般而言,需要看明白的有第一行:异常的简单信息(异常类型,异常的描述等)

  最后一行:异常出现的位置(类->方法->方法具体的行)

  getMessage:返回异常的描述信息

  (3)异常类型不匹配

  (4)多重catch

public class Test03 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入第一个数:");

		int num1 = 0;
		int num2 = 0;
		
		try {
			num1 = sc.nextInt();

			System.out.println("请输入第二个数:");
			num2 = sc.nextInt();

			int r = num1 / num2;
			System.out.println("num1/num2 = " + r);
		}catch (ArithmeticException e) {
			System.out.println("数学计算异常:"+e.getMessage());
		}catch(InputMismatchException e) {
			System.out.println("输入不匹配异常:"+e.getMessage());
		}catch (Exception e) {
			System.out.println("发送异常:"+e.getMessage());
		}
		
		System.out.println("程序正常结束");
	}
}

  

    1.1.2  try/catch/finally

    把有可能产生异常的代码放入try中,catch负责匹配并处理异常,finally块用于进行收尾工作(关闭数据库、关闭文件、释放内存等资源),不管程序是否发生异常,finally都会执行。

public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入第一个数:");

		int num1 = 0;
		int num2 = 0;

		try {
			num1 = sc.nextInt();

			System.out.println("请输入第二个数:");
			num2 = sc.nextInt();

			int r = num1 / num2;
			System.out.println("num1/num2 = " + r);
		} catch (Exception e) {
			System.out.println("程序出现异常");
		} finally {
			System.out.println("不管是否出现异常,finally都执行");
		}

		System.out.println("程序正常结束");

    其中有特殊情况

  (1)System.exit (0)正常退出jvm,finally不会执行。

  (2)catch可以省略,变成try...finally块。

    1.1.3 return

  存在return的try/catch/finally的执行顺序

package cn.sxt02.exception03;

/**
 * 存在return的情况
 */
public class Test02 {

	public static int div(int a, int b) {

		try {
			int r = a / b;
			return r;

		} catch (Exception e) {
			System.out.println("出现异常");

			return 0;

		} finally {
			System.out.println("我是finally");
		}

	}

	public static void main(String[] args) {

		int r = Test02.div(10, 0);
		System.out.println("r=" + r);
		System.out.println("程序正常结束");
	}
}

1.2 异常的分类 

Throwable 类是 Java 语言中所有错误(Error)或异常(Exception)的父类,只有当对象是此类(或其子类之一)的实例时,才能通过 Java 虚拟机或者 Java throw 语句抛出。

Error 类表示错误类。仅靠程序本身无法恢复的严重错误。jvm内存耗尽、jvm崩溃等。
Exception 类表示异常类,可以通过java 异常处理机制处理。

Exception 根据是否处理分为两种情况。
RuntimeException:运行时异常。不要求程序必须做出处理。是所有运行时异常的父类。
CheckedException:检查时异常。要求程序必须处理,不处理编译不通过。

常见的运行时异常
ArithmeticException:数学计算异常。比如除数为0
InputMismatchException:输入不匹配异常
ArrayIndexOutofBoundsException:数组下标越界异常。
NullPointException:空指针异常,对象没有初始化就使用时,jvm会抛出该异常
IllegalArgumentException:非法参数异常。
ClassCastException:强制类型转换异常。
NumberFormatException:数字格式化异常。比如把“abc”格式化成数字。

常见的检查时异常:
ClassNotFoundException:类没有被发现异常。
SQLException:数据库相关异常
IOException:IO操作异常
ParseException:解析错误异常
FileNotFoundException:文件未发现异常。

运行时异常和检查时异常的区别
运行时异常:包括RuntimeException及其所有子类。不要求程序必须对它们作出处理,比如InputMismatchException、ArithmeticException、NullPointerException等。即使没有使用try-catch或throws进行处理,仍旧可以进行编译和运行。如果运行时发生异常,会输出异常的堆栈信息并中止程序执行。
Checked异常(非运行时异常):除了运行时异常外的其他异常类都是Checked异常。程序必须捕获或者声明抛出这种异常,否则出现编译错误,无法通过编译。处理方式包括两种:通过try-catch捕获异常,通过throws声明抛出异常从而交给上一级调用方法处理

原文地址:https://www.cnblogs.com/w312308542/p/10777227.html