博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java学习记录之异常
阅读量:4048 次
发布时间:2019-05-25

本文共 6640 字,大约阅读时间需要 22 分钟。

异常的分类

  1. 自己需要处理的
try, catch, finally
  1. 让别人处理的
throws

在这里插入图片描述

实际的例子

Test.java

public class Test {
public static void main (String args[]) {
int m = Integer.parseInt(args[0]); int n = Integer.parseInt(args[1]); int r = div(m, n); System.out.println("m/n=" + r); } public static int div(int m, int n) {
int r = 0; r= m/n; return r; }}

执行 java Test 9 0会抛出算数异常,那怎么捕捉呢?

public class Test {
public static void main (String args[]) {
int m = Integer.parseInt(args[0]); int n = Integer.parseInt(args[1]); int r = div(m, n); System.out.println("m/n=" + r); } public static int div(int m, int n) {
int r = 0; try {
r = m/n; } catch (ArithmeticException e) {
System.out.println(e); } return r; }}

执行结果

lydia@lydia:~/sgy/java_learn/exception$ java Test 9 0java.lang.ArithmeticException: / by zerom/n=0

finally就是不管是否发生异常,都会最终执行到的语句!

加上finally看一下效果!

public static int div(int m, int n) {
int r = 0; try {
r = m/n; } catch (ArithmeticException e) {
System.out.println(e); } finally {
System.out.println("this is finally!"); } return r; }

执行的结果,9/1, 或者9/0最终都会执行finally的语句

lydia@lydia:~/sgy/java_learn/exception$ javac Test.javalydia@lydia:~/sgy/java_learn/exception$ java Test 9 1this is finally!m/n=9lydia@lydia:~/sgy/java_learn/exception$ java Test 9 0java.lang.ArithmeticException: / by zerothis is finally!m/n=0

怎么让别人处理

例如div函数自己本身不处理,扔给main方法

public class Test {
public static void main (String args[]) {
int m = Integer.parseInt(args[0]); int n = Integer.parseInt(args[1]); int r = 0; try {
r = div(m, n); } catch (ArithmeticException e) {
System.out.println(e); } System.out.println("m/n=" + r); } public static int div(int m, int n) throws ArithmeticException {
int r = 0; r = m / n; return r; }}

main和div怎么一起处理这个异常?

现在的程序只会在div中处理这个异常!

Test.java

public class Test {
public static void main (String args[]) {
int m = Integer.parseInt(args[0]); int n = Integer.parseInt(args[1]); int r = 0; try {
r = div(m, n); } catch (ArithmeticException e) {
System.out.println("main-->"+e); } System.out.println("m/n=" + r); } public static int div(int m, int n) throws ArithmeticException {
int r = 0; try {
r = m / n; } catch (ArithmeticException e) {
System.out.println("div--->"+ e); } return r; }}

执行的结果只有div会处理这个异常

lydia@lydia:~/sgy/java_learn/exception$ java Test 9  0div--->java.lang.ArithmeticException: / by zerom/n=0

需要通过手动抛出异常!

修改后的代码!

public static int div(int m, int n) throws ArithmeticException {
int r = 0; try {
r = m / n; } catch (ArithmeticException e) {
System.out.println("div--->"+ e); throw e; } return r; }
lydia@lydia:~/sgy/java_learn/exception$ java Test 9  0div--->java.lang.ArithmeticException: / by zeromain-->java.lang.ArithmeticException: / by zerom/n=0

异常太多,不需要一个个列出来,捕获父类异常,或者大类的异常即可

Test.java

public class Test {
public static void main (String args[]) {
int m = 0; int n =0; int r = 0; try {
m = Integer.parseInt(args[0]); n = Integer.parseInt(args[1]); r = div(m, n); } catch (ArithmeticException e) {
System.out.println("main-->"+e); } catch (RuntimeException e) {
System.out.println("main-->"+e); } System.out.println("m/n=" + r); } public static int div(int m, int n) throws ArithmeticException {
int r = 0; try {
r = m / n; } catch (ArithmeticException e) {
System.out.println("div--->"+ e); throw e; } return r; }}

执行结果

lydia@lydia:~/sgy/java_learn/exception$ java Test 9  0div--->java.lang.ArithmeticException: / by zeromain-->java.lang.ArithmeticException: / by zerom/n=0lydia@lydia:~/sgy/java_learn/exception$ java Test 9  amain-->java.lang.NumberFormatException: For input string: "a"m/n=0lydia@lydia:~/sgy/java_learn/exception$ java Test 9main-->java.lang.ArrayIndexOutOfBoundsException: 1m/n=0

人为的new execption

Test.java

div函数改成下面这样!

public static int div(int m, int n)  {
int r = 0; try {
r = m / n; } catch (ArithmeticException e) {
System.out.println("div--->"+ e); throw new Exception("My error!"); } return r; }

编译不通过,new Execption 必须处理或者抛出!

lydia@lydia:~/sgy/java_learn/exception$ javac Test.javaTest.java:27: 错误: 未报告的异常错误Exception; 必须对其进行捕获或声明以便抛出                        throw new Exception("My error!");                        ^1 个错误

继续更改,div函数

public static int div(int m, int n) throws Exception{
int r = 0; try {
r = m / n; } catch (ArithmeticException e) {
System.out.println("div--->"+ e); throw new Exception("My error!"); } return r; }

编译结果还是一样,Main函数里面也需要处理!

lydia@lydia:~/sgy/java_learn/exception$ javac Test.javaTest.java:11: 错误: 未报告的异常错误Exception; 必须对其进行捕获或声明以便抛出                        r = div(m, n);                               ^1 个错误

对Main方法增加 catch Exception,处理这个异常,就不会有问题

public class Test {
public static void main (String args[]) {
int m = 0; int n =0; int r = 0; try {
m = Integer.parseInt(args[0]); n = Integer.parseInt(args[1]); r = div(m, n); } catch (ArithmeticException e) {
System.out.println("main-->"+e); } catch (RuntimeException e) {
System.out.println("main-->"+e); } catch (Exception e) {
System.out.println("main-->"+e); } System.out.println("m/n=" + r); } public static int div(int m, int n) throws Exception{
int r = 0; try {
r = m / n; } catch (ArithmeticException e) {
System.out.println("div--->"+ e); throw new Exception("My error!"); } return r; }}

执行的结果

lydia@lydia:~/sgy/java_learn/exception$ java Test 9  0div--->java.lang.ArithmeticException: / by zeromain-->java.lang.Exception: My error!m/n=0

或者直接在div当中处理掉!

在div中增加try catch的语句,直接本方法处理!

public static int div(int m, int n) throws Exception{
int r = 0; try {
r = m / n; } catch (ArithmeticException e) {
System.out.println("div--->"+ e); try {
throw new Exception("My error!"); } catch (Exception e1) {
System.out.println("div-->"+e1); } } return r; }
lydia@lydia:~/sgy/java_learn/exception$ java Test 9  0div--->java.lang.ArithmeticException: / by zerodiv-->java.lang.Exception: My error!m/n=0

PS:

  1. try或catch块中有return或throw语句,会先执行finally块,再返回来执行return或throw语句

  2. 对于“不可查异常”, 系统也会抛出它,写不写throws效果一样

    即throws ArithmeticException不写, main方法仍能够正常捕捉到!会自动抛出

public static int div(int m, int n) throws ArithmeticException {

执行的结果

lydia@lydia:~/sgy/java_learn/exception$ java Test 9  0div--->java.lang.ArithmeticException: / by zeromain-->java.lang.ArithmeticException: / by zerom/n=0

转载地址:http://thyci.baihongyu.com/

你可能感兴趣的文章
LED恒流驱动芯片
查看>>
驱动TFT要SDRAM做为显示缓存
查看>>
使用file查看可执行文件的平台性,x86 or arm ?
查看>>
qt5 everywhere 编译summary
查看>>
qt 创建异形窗体
查看>>
可重入函数与不可重入函数
查看>>
简单Linux C线程池
查看>>
内存池
查看>>
输入设备节点自动生成
查看>>
GNU hello代码分析
查看>>
Qt继电器控制板代码
查看>>
wpa_supplicant控制脚本
查看>>
gstreamer相关工具集合
查看>>
RS232 四入四出模块控制代码
查看>>
linux 驱动开发 头文件
查看>>
container_of()传入结构体中的成员,返回该结构体的首地址
查看>>
linux sfdisk partition
查看>>
ipconfig,ifconfig,iwconfig
查看>>
opensuse12.2 PL2303 minicom
查看>>
网络视频服务器移植
查看>>