文章目录
0..来源1. 异常的分类2.一般规范3.实例说明3.1 Example1.java3.2 Example2.java3.3 Example3.java3.4 Example4.java3.5 Example5.java3.6 Example6.java3.7 Example7.java3.8 Example8.java3.9 Example9.java
0…来源
B站视频代码
1. 异常的分类
非运行时异常,必须处理运行时异常,可能出现的地方也要处理
2.一般规范
先讲规范,再讲实例不要在 try 中关闭资源 Example1
在finally 中或者使用 Java8的语法糖 try(xxx){}
指定具体异常,不要直接使用 Exception抛出 Example2给异常加点说明,比如说是什么执行错误 Example3使用描述性信息抛出异常 使用 log4j 记录描述信息和异常 Example4有多个异常优先捕获具体异常 Example5不要捕获 Throwable 异常,因为这个包括了 Exception 和 Error Example 6不要忽略异常 Example7不要记录了异常同时又抛出 Example8包装异常不要丢弃原始异常 Example9我的理解
在方法内处理异常,指明具体异常以及说明 异常记录进日志就不要抛出了? 包装异常不要丢弃原始的异常
3.实例说明
3.1 Example1.java
public class Example1 {
private static final Logger log
= LoggerFactory
.getLogger(Example1
.class);
public void doNotCloseResourceInTry() {
FileInputStream inputStream
= null
;
try {
File file
= new File("./王爵的私有宝贝.txt");
inputStream
= new FileInputStream(file
);
inputStream
.close();
} catch (FileNotFoundException e
) {
log
.error("", e
);
} catch (IOException e
) {
log
.error("", e
);
}
}
public void closeResourceInFinally() {
FileInputStream inputStream
= null
;
try {
File file
= new File("./王爵的私有宝贝.txt");
inputStream
= new FileInputStream(file
);
} catch (FileNotFoundException e
) {
log
.error("", e
);
} catch (IOException e
) {
log
.error("", e
);
} finally {
if(null
!= inputStream
){
try {
inputStream
.close();
} catch (IOException e
) {
log
.error("关闭流失败", e
);
}
}
}
}
public void autoCloseResource() {
File file
= new File("./王爵的私有宝贝.txt");
try (FileInputStream inputStream
= new FileInputStream(file
)){
} catch (FileNotFoundException e
) {
log
.error("", e
);
} catch (IOException e
) {
log
.error("", e
);
}
}
}
3.2 Example2.java
public class Example2 {
public void doNotDoThis() throws Exception
{
Integer year
= Integer
.parseInt("biezhi???");
System
.out
.println("哦 " + year
);
}
public void doThis() throws NumberFormatException
{
Integer year
= Integer
.parseInt("biezhi???");
System
.out
.println("哦 " + year
);
}
}
3.3 Example3.java
public class Example3 {
class NotFoundGirlFriendException extends Exception {
public NotFoundGirlFriendException(String message
) {
super(message
);
}
}
public void doSomething(String input
) throws NotFoundGirlFriendException
{
}
}
3.4 Example4.java
public class Example4 {
private static final Logger log
= LoggerFactory
.getLogger(Example4
.class);
public void foo() {
try {
new Long("biezhi大法好");
} catch (NumberFormatException e
) {
log
.error("用户ID格式化失败", e
);
}
}
}
3.5 Example5.java
public class Example5 {
private static final Logger log
= LoggerFactory
.getLogger(Example5
.class);
public void catchMostSpecificExceptionFirst() {
try {
doSomething("不要跟我讲这些,因为我只是一只小猫咪!");
} catch (NumberFormatException e
) {
log
.error("字符串转数字失败", e
);
} catch (IllegalArgumentException e
) {
log
.error("非法参数", e
);
}
}
private void doSomething(String message
) {
Integer emmm
= Integer
.parseInt(message
);
}
public static void main(String
[] args
) {
new Example5().catchMostSpecificExceptionFirst();
}
}
3.6 Example6.java
public class Example6 {
public void doNotCatchThrowable() {
try {
} catch (Throwable t
) {
}
}
}
3.7 Example7.java
public class Example7 {
private static final Logger log
= LoggerFactory
.getLogger(Example7
.class);
public void doNotIgnoreExceptions() {
try {
} catch (NumberFormatException e
) {
log
.error("不可能走到的", e
);
}
}
}
3.8 Example8.java
public class Example8 {
private static final Logger log
= LoggerFactory
.getLogger(Example8
.class);
public void foo() {
try {
new Long("xyz");
} catch (NumberFormatException e
) {
throw e
;
}
}
public static void main(String
[] args
) {
new Example8().foo();
}
}
3.9 Example9.java
public class Example9 {
class MyBusinessException extends Exception {
public MyBusinessException(String message
) {
super(message
);
}
public MyBusinessException(String message
, Throwable cause
) {
super(message
, cause
);
}
}
public void wrapException(String id
) throws MyBusinessException
{
try {
long userId
= Long
.parseLong(id
);
System
.out
.println("userId: " + userId
);
} catch (NumberFormatException e
) {
throw new MyBusinessException("描述错误的消息", e
);
}
}
public static void main(String
[] args
) throws MyBusinessException
{
new Example9().wrapException("emmm");
}
}