异常是什么?
- 程序运行时有着太多的不确定性,比如写好一段读取文件的程序,下一次运行时文件被人给删除了。程序要第一时间给程序员这个文件不存在的消息。这个因为不确定性而导致程序无法运行时给出的消息,就是我们所说的异常。
- Java中万事万物皆对象,因此这个异常消息,也被Java定义成了对象(Exception)
异常体系图
异常分类
- 受检异常。。。。啥含义?
- 运行时异常。。。啥含义?
异常常见方法
- getMessage()
- 返回抛出异常的详细信息
- printStackTrace()
- 打印异常信息到标准输出流上
常见受检异常
常见运行时异常
try-catch-finally
- 代码块
Demo1.java
package course.basic.exception;
import java.io.IOException;
public class Demo1 {
public static void main(String[] args) throws IOException {
testRuntimeException();
foo();
}
public static void foo() throws IOException {
testCheckedException();
}
public static void testRuntimeException() throws NullPointerException {
System.out.println("Demo1.testRuntimeException");
}
public static void testCheckedException() throws IOException {
System.out.println("Demo1.testCheckedException");
}
}
Demo2.java
import java.io.*;
public class Demo2 {
public static void main(String[] args) {
// foo1();
// foo2();
/**
* jdk 1.7之前
* try{
*
* }catch(XxxException e){
*
* }finally{
*
* }
*
* jdk1.7新特性
* try(){
*
* }catch(XxxException e){
*
* }
*
*
*/
}
public static void foo3() {
// try() 写法必须是类要实现自 Closeable 接口
// try (Foo2 foo = new Foo2()) {
//
// } catch (Exception e) {
// e.printStackTrace();
// }
}
static class Foo1 implements Closeable {
@Override
public void close() throws IOException {
}
}
static class Foo2 {
}
public static void foo2() {
String path = "/Users/haoc/course/code/cakes-course/basic/src/main/java/course/basic/exception/Demo2.jaa";
try (BufferedReader reader = new BufferedReader(new FileReader(new File(path)))) {
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (FileNotFoundException e) {
System.out.println("getMessage:" + e.getMessage());
System.out.println("---------");
e.printStackTrace();
// System.out.println("Demo2.FileNotFoundException");
} catch (IOException e) {
e.printStackTrace();
System.out.println("Demo2.IOException");
}
}
public static void foo1() {
String path = "/Users/haoc/course/code/cakes-course/basic/src/main/java/course/basic/exception/Demo2.jaa";
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(new File(path)));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (FileNotFoundException e) {
System.out.println("getMessage:" + e.getMessage());
System.out.println("---------");
e.printStackTrace();
// System.out.println("Demo2.FileNotFoundException");
} catch (IOException e) {
e.printStackTrace();
System.out.println("Demo2.IOException");
} finally {
if (null != reader) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Demo3.java
import java.io.Closeable; import java.io.IOException; /** * */ public class Demo3 { public static void main(String[] args) { try (Foo1 foo = new Foo1()) { } catch (Exception e) { e.printStackTrace(); } } static class Foo1 implements Closeable { @Override public void close() throws IOException { } } }
Demo4.java
public class Demo4 { public static void main(String[] args) { } public static void foo1() { // send http request try { // timeout 异常 1 // String response = HttpClient.doPost(url,params); // response handle // response.data.bizCode = 余额不足 异常 2 // response.data.bizCode = 支付系统故障 3 // } catch (TimeoutEx e) { // // retry // } catch (BalanceEx e) { // // ignore // } catch (SysEx e) { // // send alarm // throw e; // } } } catch (Exception e) { // log.error("failed. push data. 余额不足") // 三类异常 StackTraceElement[] stackTraceArr = e.getStackTrace(); for (StackTraceElement stackTraceElement : stackTraceArr) { stackTraceElement.getMethodName(); stackTraceElement.getClassName(); stackTraceElement.getLineNumber(); } handleException(e); } } static void handleException(Exception e) { if (e instanceof TimeoutEx) { // retry } if (e instanceof BalanceEx) { // ignore } if (e instanceof SysEx) { // log.error() } } /** * 自定义异常 */ static class TimeoutEx extends RuntimeException { public TimeoutEx() { } public TimeoutEx(String message) { super(message); } public TimeoutEx(String message, Throwable cause) { super(message, cause); } public TimeoutEx(Throwable cause) { super(cause); } public TimeoutEx(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } } static class BalanceEx extends RuntimeException { } static class SysEx extends RuntimeException { } }