Java学习之异常处理的两种方式

Java学习之异常处理的两种方式

异常处理有两种方式,分别是

1.向调用方向传播异常的声明抛出异常处理

2.在当前方法捕获处理异常的程序捕获处理
在这里插入图片描述


一、声明抛出异常处理

1.显示声明抛出 运用throws语句

举例:IOException

在没有显示声明抛出的时候,代码如下:

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class TestScreenIn {
    public static void main(String[] args) {
        BufferedReader keyin = new BufferedReader(new InputStreamReader(System.in));
        String c1;
        int i = 0;
        String[] e = new String[10];
        while (i < 10) {
            c1 = keyin.readLine();
            e[i] = c1;
            i++;
        }
    }
}

该程序会在下述位置显示错误:

 c1 = keyin.readLine();//Unhandled exception: java.io.IOException

当采用显示声明异常抛出后,代码如下:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class TestScreenIn {
    public static void main(String[] args) throws IOException{
        BufferedReader keyin = new BufferedReader(new InputStreamReader(System.in));
        String c1;
        int i = 0;
        String[] e = new String[10];
        while (i < 10) {
            c1 = keyin.readLine();
            e[i] = c1;
            i++;
        }
    }
}

thorws IOException 向上抛出

向上抛出:将异常抛给调用方

本例将IOException异常抛出给调用main方法的java虚拟机

2.隐式声明抛出 throws语句可省略

举例:NULLPointerException

public class TestArray {
 private static int[] x;
 public  static void main(String[] args){
     System.out.println(x[0]);
 }
}

会抛出如下错误:

Exception in thread "main"
java.lang.NullPointerException
at TestArray.main(TestArray.java:4)

改正:在使用前可以先进行判空操作

if(x==null){
  //
}

举例:ArrayIndexOutOfBoundsException

public class TestArgs {
 public static void main( String[] args) {
    String foo = args[1]; 
    System.out.println("foo = " + foo);
 }
}

会抛出如下错误:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at TestArgs.main(TestArgs.java:3)

改正:在使用前进行判空操作

二、程序捕获处理

采用try/catch/finally语句

一般形式如下:

try {
  statements
}
catch (ExceptionType1 ExceptionObject){
  Exception Handling 
}
catch (ExceptionType2 ExceptionObject) { 
  Exception Handling
}……
finally{
  Finally  Handling 
}

多catch情况: 异常类由上到下排布规则是由子到父,由具体到抽象,或为并列关系。执行过程类似于switch case(但一个执行,其它不执行)。若无异常发生,则都不执行。

finally: 是这个组合语句的统一出口,一般用来进行一些善后清理操作,例如清理资源、释放连接、关闭文件、管道流等操作。它是可选的部分,但一旦选定,必定执行。

举例:

public class TestFinally {
    public static void main(String[] args) {
       System.out.println(testFinally());
    }
    private static int testFinally() {
       int temp = 100;
       try {
          System.out.println("Try");
          return ++temp;// return的结果被暂存
    }
    finally {
       temp = 1; 
       System.out.println( "Finally" );
 }}}
//输出结果为:
//Try
//Finally
//101

finally代码块在return语句运行后执行的。return结果被暂存,等finally代码块执行结束后再将之前暂存的结果返回

finall用途: 一般用来进行一些善后清理操作, 例如清理资源、释放连接、关闭文件、I/O流等操作。

举例:

String readFileWithFinallyBlock(String path) throws IOException {
     BufferedReader br = new BufferedReader(new FileReader(path));
     try {
        return br.readLine();
     } 
     finally {
        if (br != null) br.close(); //关闭文件流
     }
    }
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值