一、异常和错误
错误:在我们编写程序的过程中会经常发生的,包括编译期间和运行期间的错误。
异常:在程序运行过程中,意外发生的情况,背离我们程序本身的意图的表现,都可以理解为异常。
二、异常分类
- Throwable(异常顶层)
- Error(错误):程序无法处理的错误,表示运行应用程序中较严重的问题。
- Exception(异常):程序本身可以处理的异常。异常处理通常指针对这种类型异常的处理。
- Unchecked Exception:非检查异常
- Checked Exception:检查异常

三、异常处理
通过五个关键字来实现:try、catch、finally、throw(手动抛出异常,通常配合try catch或throws使用)、throws
(一)try…catch…
try{
// 执行后可能产生异常的代码
}
catch (/*异常捕捉*/){
// 异常处理
}
finally{
// 不论是否发生异常,都会执行的代码
}
案例如下:
public class TestMain {
public void compute() {
int x = 10, y = 0;
double z = 0;
try {
z = x / y;
System.out.println(z);
} catch (ArithmeticException e) {
System.out.println("除数不得为0");
} catch (Exception e) { // 通常会在最后加上这个,来处理可能被遗漏的异常
e.printStackTrace();
}
}
public static void main(String[] args) {
TestMain testMain = new TestMain();
testMain.compute();
}
}
(二)throws
public void method() throws 异常类型{
// 执行后可能产生异常的代码
}
案例如下
public class TestMain {
public void compute2() throws ArithmeticException{
int x = 10, y = 0;
double z = 0;
z = x / y;
System.out.println(z);
}
public static void main(String[] args) {
TestMain testMain = new TestMain();
try {
testMain.compute2();
} catch (Exception e) {
System.out.println("除数不得为0");
}
}
}
(三)throw
-
配合try…catch…
public class TestMain { public void compute3() { int x = 10, y = 0; double z = 0; try { if (y == 0) { throw new Exception("除数不能为0"); }else { z = x / y; System.out.println(z); } } catch (ArithmeticException e) { System.out.println("除数不得为00"); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { TestMain testMain = new TestMain(); testMain.compute3(); } } /* 抛出异常: java.lang.Exception: 除数不能为0 */ -
配合throws
public class TestMain { public void compute4() throws Exception{ int x = 10, y = 0; double z = 0; if (y == 0) { throw new Exception("除数不能为0"); }else { z = x / y; System.out.println(z); } } public static void main(String[] args) { TestMain testMain = new TestMain(); try { testMain.compute4(); } catch (Exception e) { System.out.println("除数不得为0"); } } } /* 运行结果: 除数不得为0 */
四、自定义异常
取用上面的案例
public class DivException extends Exception {
public DivException() {
super("除数不得为0");
}
}
public class TestMain {
// throw与try catch
public void compute3() {
int x = 10, y = 0;
double z = 0;
try {
if (y == 0) {
throw new DivException(); // 抛出自定义异常
}else {
z = x / y;
System.out.println(z);
}
} catch (ArithmeticException e) {
System.out.println("除数不得为00");
} catch (Exception e) {
e.printStackTrace();
}
}
public void compute4() throws Exception{
int x = 10, y = 0;
double z = 0;
if (y == 0) {
throw new DivException(); // 抛出自定义异常
}else {
z = x / y;
System.out.println(z);
}
}
// throw与throws搭配
public static void main(String[] args) {
TestMain testMain = new TestMain();
testMain.compute3();
try {
testMain.compute4();
} catch (Exception e) {
System.out.println("除数不得为0");
}
}
}
五、异常链
异常链:将异常发生的原因一个传一个串起来,即将底层异常信息传给上层,这样逐层抛出。
public class TestMain {
public void compute() throws DivException{
int x = 10, y = 0;
double z = 0;
try {
z = x / y;
System.out.println(z);
} catch (ArithmeticException e) {
throw new DivException();
}
}
public void testExcept1() throws Exception{
try {
compute();
} catch (Exception e) {
throw new Exception("新异常1", e);
}
}
public void testExcept2() throws Exception{
try {
testExcept1();
} catch (Exception e) {
throw new Exception("新异常2", e);
}
}
public static void main(String[] args) {
TestMain testMain = new TestMain();
try {
testMain.testExcept2();
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行结果:

如果将代码中的y = 0改成y = 2,则运行结果为5.0。
本文详细介绍了Java中的异常处理机制,包括异常与错误的区别、异常的分类、如何使用try-catch-finally、throws和throw进行异常处理,以及如何自定义异常和使用异常链。

被折叠的 条评论
为什么被折叠?



