一、单一异常处理
package exception;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class TestException {
public static void main(String[] args) {
String fileName = "d:/fileTest.txt";
File f = new File(fileName);
try{
System.out.format("准备打开:%s\n",fileName);
new FileInputStream(f);
System.out.format("成功打开:%s\n",fileName);
}
catch(Exception e){//可以使用父类抛出异常
//catch(FileNotFoundException e){
System.out.format("打开%s失败\n",fileName);
e.printStackTrace();
}
}
}
运行结果:
准备打开:d:/fileTest.txt
成功打开:d:/fileTest.txt
二、多异常处理
1. 方法一:分别catch
需要抛出两个错误时,使用父类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) {
String fileName = "d:/fileTest.txt";
String dateTest = "2019-3-28";
File f = new File(fileName);
try{
System.out.format("准备打开:%s\n",fileName);
new FileInputStream(f);
System.out.format("成功打开:%s\n",fileName);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date d = sdf.parse(dateTest);
System.out.format("成功转换%s,转换为%s", dateTest,d);
}catch(FileNotFoundException e){
//catch(Exception e){//可以使用父类抛出异常
System.out.format("打开%s失败\n",fileName);
e.printStackTrace();
}catch(ParseException e){//需要抛出两个错误时,使用父类抛出错误时会报错
System.out.println("转化日期失败");
e.printStackTrace();
}
}
}
2. 方法二:放在一个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) {
String fileName = "d:/fileTest.txt";
String dateTest = "2019-3-28";
File f = new File(fileName);
try{
System.out.format("准备打开:%s\n",fileName);
new FileInputStream(f);
System.out.format("成功打开:%s\n",fileName);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date d = sdf.parse(dateTest);
System.out.format("成功转换%s,转换为%s", dateTest,d);
}catch(FileNotFoundException | ParseException e){
//catch(Exception e){//可以使用父类抛出异常
if (e instanceof FileNotFoundException){
System.out.format("打开%s失败\n",fileName);
e.printStackTrace();
}
if (e instanceof ParseException){
System.out.println("转化日期失败");
e.printStackTrace();
}
}
}
}
结果:
准备打开:d:/fileTest.txt
成功打开:d:/fileTest.txt
成功转换2019-3-28,转换为Thu Mar 28 00:00:00 CST 2019
三、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) {
String fileName = "d:/fileTest.txt";
String dateTest = "2019-3-28";
File f = new File(fileName);
try{
System.out.format("准备打开:%s\n",fileName);
new FileInputStream(f);
System.out.format("成功打开:%s\n",fileName);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date d = sdf.parse(dateTest);
System.out.format("成功转换%s,转换为%s\n", dateTest,d);
}
catch(FileNotFoundException | ParseException e){
//catch(Exception e){//可以使用父类抛出异常
if (e instanceof FileNotFoundException){
System.out.format("打开%s失败\n",fileName);
e.printStackTrace();
}
if (e instanceof ParseException){
System.out.println("转化日期失败\n");
e.printStackTrace();
}
}
finally{
System.out.println("都会执行该段代码");
}
}
}
结果:
准备打开:d:/fileTest.txt
成功打开:d:/fileTest.txt
成功转换2019-3-28,转换为Thu Mar 28 00:00:00 CST 2019
都会执行该段代码
四、throws
将问题throws,当调用的时候才处理异常,此处method1也可以throws,在main函数里处理异常。
package exception;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class ExceptionTest {
public static void main(String[] args) {
method1();
}
private static void method1(){
try{
method2();
}
catch(FileNotFoundException e){
e.printStackTrace();
}
finally{
System.out.println("肯定执行");
}
}
private static void method2() throws FileNotFoundException{
File f = new File("d:/fileTest.txt");
System.out.println("尝试打开:");
new FileInputStream(f);
System.out.println("success");
}
}
五、异常分类
1. 错误
不是必须catch的异常,通常为内存溢出,OutOfMemoryError
2. 运行时异常
即不是必须catch的异常,包括除数为0,下标越界、空指针等
3.可查异常
即必须进行处理的异常,要么try catch住,要么往外抛,谁调用,谁处理,如文件读取、日期转换。
六、Throwable
Throwable是类,Exception和Error都继承了该类,所以在捕捉的时候,也可以使用Throwable进行捕捉。
try{
method2();
}
catch(Throwable t){
t.printStackTrace();
}
图片来源:how2j.cn
七、自定义异常
1. 构造EnemyHeroIsDeadException类
在被攻击英雄血量为0时,无法被攻击。
package exception;
public class EnemyHeroIsDeadException extends Exception{
public EnemyHeroIsDeadException(){}
public EnemyHeroIsDeadException(String msg){
super(msg);//调用父类的对应的构造方法
}
}
2. 抛出EnemyHeroIsDeadException
package lol;
import exception.EnemyHeroIsDeadException;
public class Hero {
String name;
float hp;
public Hero(String name,float hp){
this.name = name;
this.hp = hp;
}
public Hero(){}
public void attackHero(Hero h) throws EnemyHeroIsDeadException{
if(h.hp <= 0){
throw new EnemyHeroIsDeadException(h.name + "hp=0,无法攻击");
}
}
public static void main(String[] arg){
Hero temmo = new Hero("提莫",250);
Hero garen = new Hero("盖伦",450,45,360);
teemo.hp = teemo.hp - 400;
try{
garen.attackHero(teemo);
}
catch(EnemyHeroIsDeadException e){
System.out.println("异常原因:" + e.getMessage());
e.printStackTrace();
}
}
}
结果:
异常原因:提莫hp=0,无法攻击
exception.EnemyHeroIsDeadException: 提莫hp=0,无法攻击
at lol.Hero.attackHero(Hero.java:33)
at lol.Hero.main(Hero.java:52)
-17.0