Java中级内容——异常处理(exception handing)

Java中级内容——异常处理(exception handing)

声明:文章为https://how2j.cn/k/exception/exception-tutorial/332.html网站内容的学习笔记。里面有更加详细的视频和解读。本文仅为自己学习、练习、复习的一个参考。
一、什么是异常
异常定义:
导致程序的正常流程被中断的事件,叫做异常。

package Exception;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class TestException {
	public static void main(String[] args) {
		
		File f = new File("d:/LOL.exe"); // 试图打开文件LOL.exe,会抛出FileNotFoundException,如果不处理该异常,就会有编译错误
		
		try {
			new FileInputStream(f);
			System.out.println("LOL.exe文件正常打开");
		}
		
		catch(FileNotFoundException e) {
			System.out.println("LOL.exe文件找不到,请检查");
			e.printStackTrace();
			
		}
		
	}
}

输出结果:
LOL.exe文件找不到,请检查
java.io.FileNotFoundException: d:\LOL.exe (系统找不到指定的文件。)
	at java.io.FileInputStream.open0(Native Method)
	at java.io.FileInputStream.open(Unknown Source)
	at java.io.FileInputStream.<init>(Unknown Source)
	at Exception.TestException.main(TestException.java:13)

知识点:如何打开一个文件。注意导入的三个包File,FileInputStream,FileNotFoundException.

问题:try_catch没学过,不知道用法。

二、如何处理

异常处理常见手段: try catch finally throws

1、try catch
1.将可能抛出FileNotFoundException 文件不存在异常的代码放在try里
2.如果文件存在,就会顺序往下执行,并且不执行catch块中的代码
3. 如果文件不存在,try 里的代码会立即终止,程序流程会运行到对应的catch块中
4. e.printStackTrace(); 会打印出方法的调用痕迹,如此例,会打印出异常开始于TestException的那一行,这样就便于定位和分析到底哪里出了异常。Stack指的是栈调用,从输出的结果信息最下面层层调用。

2、使用异常的父类进行catch
FileNotFoundException是Exception的子类,使用Exception也可以catch住FileNotFoundException。

问题:
1.按F3查看FileNotFoundException源文件时,出现the jar file has no source attachment,无法查看源码。参考解决方法,顺利解决问题。
2.输出结果显示中有Unknown Source,无法查找到源码中对应的程序。

package Exception;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class TestException {
	public static void main(String[] args) {
		
		File f = new File("d:/LOL.exe"); // 试图打开文件LOL.exe,会抛出FileNotFoundException,如果不处理该异常,就会有编译错误
		
		try {
			new FileInputStream(f);
			System.out.println("LOL.exe文件正常打开");
		}
		
		//catch(FileNotFoundException e) {
		catch(Exception e) {
			System.out.println("LOL.exe文件找不到,请检查");
			e.printStackTrace();
			
		}
		
	}
}

3、多异常捕捉办法1
解决办法之一是分别进行catch,把可能出现问题的代码都放在try里面,然后分别进行catch。

注意:如果第二个catch是ParseException,则前面的catch不能是Exception而需要用FileNotFoundException,否则会出现错误Unreachable catch block for ParseException. It is already handled by the catch block for Exception。

package Exception;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TestException {
	public static void main(String[] args) {
		
		File f = new File("d:/LOL.exe"); // 试图打开文件LOL.exe,会抛出FileNotFoundException,如果不处理该异常,就会有编译错误
		
		try {
			new FileInputStream(f);
			System.out.println("LOL.exe文件正常打开");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date d = sdf.parse("2016-09-27");	
		}
		
		catch(FileNotFoundException e) {
			System.out.println("LOL.exe文件找不到,请检查");
			e.printStackTrace();
		}
		
		catch (ParseException e) {
            System.out.println("日期格式解析错误");
            e.printStackTrace();
        }
		
	}
}

4、多异常捕捉办法2
另一个种办法是把多个异常,放在一个catch里统一捕捉。这种方式从 JDK7开始支持,好处是捕捉的代码更紧凑,不足之处是,一旦发生异常,不能确定到底是哪种异常,需要通过instanceof 进行判断具体的异常类型。

注意:只需要写一个e.printStackTrace();就行。e引用所指对象为catch括号中错误类型对象。

package Exception;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TestException {
	public static void main(String[] args) {

		File f = new File("d:/LOL.exe"); // 试图打开文件LOL.exe,会抛出FileNotFoundException,如果不处理该异常,就会有编译错误

		try {
			new FileInputStream(f);
			System.out.println("LOL.exe文件正常打开");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date d = sdf.parse("2016-09-27");
		}

		catch (FileNotFoundException | ParseException e) {

			if (e instanceof FileNotFoundException) {
				System.out.println("LOL.exe文件找不到,请检查");
			}

			if (e instanceof ParseException) {
				System.out.println("日期格式解析错误");
			}
			e.printStackTrace();
		}

	}
}

5、finally(面试考察点)
无论是否出现异常,finally中的代码都会被执行。比如用来执行数据库的关闭工作,无论前面是否运行正常,finally都会将其关闭。

package Exception;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TestException {
	public static void main(String[] args) {

		File f = new File("d:/LOL.exe"); // 试图打开文件LOL.exe,会抛出FileNotFoundException,如果不处理该异常,就会有编译错误

		try {
			new FileInputStream(f);
			System.out.println("LOL.exe文件正常打开");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date d = sdf.parse("2016-09-27");
		}

		catch (FileNotFoundException | ParseException e) {

			if (e instanceof FileNotFoundException) {
				System.out.println("LOL.exe文件找不到,请检查");
			}

			if (e instanceof ParseException) {
				System.out.println("日期格式解析错误");
			}
			e.printStackTrace();
		}
		
		finally {
			System.out.println("无论文件是否打开,它都会被执行");
		}

	}
}

6、throws
考虑如下情况:
主方法调用method1
method1调用method2
method2中打开文件

method2中需要进行异常处理
但是method2不打算处理,而是把这个异常通过throws抛出去
那么method1就会接到该异常。 处理办法也是两种,要么是try catch处理掉,要么也是抛出去。
method1选择本地try catch住 一旦try catch住了,就相当于把这个异常消化掉了,主方法在调用method1的时候,就不需要进行异常处理了。

package Exception;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TestException {
	public static void main(String[] args) {
		method1();
	}
	
	private static void method1() {
		try {
			method2();
		}
		
		catch(FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	private static void method2() throws FileNotFoundException {
		File f = new File("d:/LOL.exe");
		 
        System.out.println("试图打开 d:/LOL.exe");
        new FileInputStream(f);
        System.out.println("成功打开");
	}
}

问题:
假设有一个方法 public int method(), 会返回一个整数
在这个方法中有try catch 和 finally.
try 里返回 1
catch 里 返回 2
finally 里 返回3
那么,这个方法到底返回多少?

无论try代码块中是否有异常,finally里的代码都会执行。try和catch代码块中有return语句时,finally仍然会执行。如果try…catch…finally都有return语句,则等待try catch执行完之后,跳过try catch里的return语句只执行finally中的return语句。如果仅仅是try…catch里有return语句,那么在执行return语句之前会先执行finally代码块里的内容。

7、throw和throws的区别
throws与throw这两个关键字接近,不过意义不一样,有如下区别:

  1. throws 出现在方法声明上,而throw通常都出现在方法体内。
  2. throws 表示出现异常的一种可能性,并不一定会发生这些异常;throw则是抛出了异常,执行throw则一定抛出了某个异常对象。

三、异常分类
异常分类: 可查异常,运行时异常和错误3种,运行时异常和错误又叫非可查异常。

1、可查异常
可查异常: CheckedException
可查异常即必须进行处理的异常,要么try catch住,要么往外抛,谁调用,谁处理,比如 FileNotFoundException,如果不处理,编译器,就不让你通过。

package Exception;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TestException {
	public static void main(String[] args) {

		File f = new File("d:/LOL.exe");

		try {
			System.out.println("试图打开 d:/LOL.exe");
			new FileInputStream(f);
			System.out.println("成功打开");
		} catch (FileNotFoundException e) {
			System.out.println("d:/LOL.exe不存在");
			e.printStackTrace();
		}

	}
}

2、运行时异常
运行时异常RuntimeException指: 不是必须进行try catch的异常
常见运行时异常:
除数不能为0异常:ArithmeticException
下标越界异常:ArrayIndexOutOfBoundsException
空指针异常:NullPointerException
在编写代码的时候,依然可以使用try catch throws进行处理,与可查异常不同之处在于,即便不进行try catch,也不会有编译错误
Java之所以会设计运行时异常的原因之一,是因为下标越界,空指针这些运行时异常太过于普遍,如果都需要进行捕捉,代码的可读性就会变得很糟糕。

public static void main(String[] args) {
         
        //任何除数不能为0:ArithmeticException
        int k = 5/0;
         
        //下标越界异常:ArrayIndexOutOfBoundsException
        int j[] = new int[5];
        j[10] = 10;
         
        //空指针异常:NullPointerException
        String str = null;
        str.length();
   }

3、错误
错误Error,指的是系统级别的异常,通常是内存用光了
在默认设置下,一般java程序启动的时候,最大可以使用16m的内存
如例不停的给StringBuffer追加字符,很快就把内存使用光了。抛出OutOfMemoryError
与运行时异常一样,错误也是不要求强制捕捉的

package Exception;

public class TestException {
	public static void main(String[] args) {

		StringBuffer sb = new StringBuffer();

		for (int i = 0; i < Integer.MAX_VALUE; i++) {
			sb.append('a');
		}

	}
}



Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

问题:
运行时异常 RuntimeException,能否被捕捉?

错误Error,能否被捕捉?

面试题常问题: 运行时异常与非运行时异常的区别

运行时异常和错误是不可查异常,可以被捕捉,不需要进行显示的捕捉。

运行时异常是不可查异常,不需要进行显示的捕捉,非运行时异常是可查异常,必须显示的捕捉或者抛出。

四、Throwable
Throwable是类,Exception和Error都继承了该类
所以在捕捉的时候,也可以使用Throwable进行捕捉
如图: 异常分Error和Exception
Exception里又分运行时异常和可查异常。
在这里插入图片描述
五、自定义异常
1、创建自定义异常

package Exception;

public class EnemyHeroIsDeadException extends Exception{
	public EnemyHeroIsDeadException() {
		
	}
	public EnemyHeroIsDeadException(String msg) {
		super(msg);
	}
}

2、抛出自定义异常
在Hero的attack方法中,当发现敌方英雄的血量为0的时候,抛出该异常

  1. 创建一个EnemyHeroIsDeadException实例
  2. 通过throw 抛出该异常
  3. 当前方法通过 throws 抛出该异常

在外部调用attack方法的时候,就需要进行捕捉,并且捕捉的时候,可以通过e.getMessage() 获取当时出错的具体原因

package Exception;

public class Hero {
	public String name;
	protected float hp;

	public void attackHero(Hero h) throws EnemyHeroIsDeadException { // 方法声明时,如果方法中有throw抛出了一个异常错误,必须声明异常

		if (h.hp == 0) {
			throw new EnemyHeroIsDeadException(h.name + " 已经挂了,不需要施放技能"); // 通过throw抛出自定义异常类的一个实例对象,没有外部类调用它,直接new EnemyHeroIsDeadException()。
		}
	}

	class EnemyHeroIsDeadException extends Exception { // 内部类,先创建一个自定义的异常类,里面有两个构造方法,
														// 一个有参,一个无参,有参构造方法调用父类中的构造方法,输出异常相关信息

		public EnemyHeroIsDeadException() {

		}

		public EnemyHeroIsDeadException(String msg) {
			super(msg);
		}

	}

	public static void main(String[] args) {

		Hero garen = new Hero();
		garen.name = "盖伦";
		garen.hp = 616;

		Hero teemo = new Hero();
		teemo.name = "提莫";
		teemo.hp = 0;

		try {
			garen.attackHero(teemo); // 抛出异常的方法放在try中

		} catch (EnemyHeroIsDeadException e) {
			// TODO Auto-generated catch block
			System.out.println("异常的具体原因:" + e.getMessage()); // 获取异常相关信息
			e.printStackTrace();
		}

	}
}

注意:首先要创建一个自定义异常,然后用throw抛出这个异常的一个实例对象,然后在方法中声明这个异常,最后对它进行捕捉。
六、异常综合练习
1、练习1
这是一个类图
Account类: 银行账号
属性: balance 余额
方法: getBalance() 获取余额
方法: deposit() 存钱
方法: withdraw() 取钱
OverdraftException: 透支异常,继承Exception
属性: deficit 透支额
在这里插入图片描述
知识点:
UML类图
外部类调用非静态内部类中的方法

package Exception;

public class Account {
	protected double balance;

	public Account(double balance) {
		this.balance = balance; // 有参数的话,要将参数赋给变量

	}

	public double getBalance() { // 读取当前余额,不论在下面哪行调用,都能获得当时的余额情况
		return balance;
	}

	public void deposit(double amt) { // 存钱,参数为本次存进钱的金额
		this.balance += amt; // 余额当前值
	}

	public void withdraw(double amt) throws OverdraftException { // 取钱,如果取钱值大于余额值,抛出一个异常,告诉透支余额这条信息并且给出透支的金额
		if (amt > this.balance) {
			double deficit = amt - this.balance; // 计算出deficit的值
			throw new OverdraftException("透支余额", deficit);// deficit给出数值或者由前面计算获得
		}

	}

	public class OverdraftException extends Exception { // 透支余额异常

		private double deficit;

		public OverdraftException(String message, double deficit) {
			super(message);
			this.deficit = deficit;// 有参数的话,要将参数赋给变量

		}

		public double getDeficit() { // 获取透支金额
			return deficit;
		}

	}

	public static void main(String[] args) {

		Account a = new Account(4000);
		System.out.println(a.getBalance());

		a.deposit(1000);
		System.out.println(a.getBalance());

		try {
			a.withdraw(6000);
		} catch (OverdraftException e) {
			System.out.println("透支金额: " + e.getDeficit()); // 这里e引用是OverdraftException对象,可以调用非静态内部类中的方法getDeficit()
			e.printStackTrace(); // 外部类无法直接调用非静态内部类中的方法,可以创建一个对象调用
		}
	}

}

2、练习2
类: CheckingAccount 支票账户,具备透支额度,继承Account
属性:overdraftProtection 透支额度
在这里插入图片描述

package Exception;

import Exception.Account.OverdraftException;

public class CheckingAccount extends Account{
	private double overdraftProtection;//透支额度
	
	public CheckingAccount(double balance) {
		super(balance);
	}
	
	public CheckingAccount(double balance, double protect) {
		super(balance);
		this.overdraftProtection = protect;
	}
	
	public void withdraw(double amt) throws OverdraftException{
		
		if(amt>this.balance +this.overdraftProtection ) {
			double deficit = amt -this.balance-this.overdraftProtection;//注意deficit是重新声明的一个变量,它在外部类中
			throw new OverdraftException("取钱超额", deficit);
		}
		
		this.balance -= amt;
	}
		
	public static void main(String[] args) {
		
		CheckingAccount c = new CheckingAccount(4000, 1000); //开户存了4000,有1000的透支额
		try {
			c.withdraw(10000);
			c.withdraw(500);
		}
		catch(OverdraftException e){
			System.out.println(e.getDeficit());
			e.printStackTrace();
		}
		
	}
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值