栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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(); //关闭文件流
     }
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/351225.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号