当前位置: 代码网 > it编程>编程语言>Java > Java异常处理机制throws举例详解

Java异常处理机制throws举例详解

2024年11月25日 Java 我要评论
一、finally的使用说明1、finally的理解1、我们将一定要被执行的代码声明在finally结构中2、更深刻的理解:无论try中或者catch中是否存在仍未被处理的异常,无论try中或catc

一、finally的使用说明

1、finally的理解

1、我们将一定要被执行的代码声明在finally结构中
2、更深刻的理解:无论try中或者catch中是否存在仍未被处理的异常,无论try中或catch中是否存在return语句等,finally中声明的语句都一定要被执行。3、finally语句和catch语句是可选的,但是finally不能单独使用
4、try-catch可以嵌套使用

2、什么样的代码我们一定要声明在finally中呢?

我们在开发中,有一些资源,比如:(输入流、输出流、数据库连接、socket连接等资源),在使用完以后,必须显式的进行关闭操作,否则,gc不会自动的回收这些资源。进而导致内存泄漏。为了保证这些资源在使用完之后,不管是否出现了未被处理的异常的情况下,这些资源能被关闭。我们必须将这些操作声明在finally中

package trycatchfinally;

import java.io.file;
import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.ioexception;

/**
 * package:trycatchfinally
 *
 * @author jimmy-yan
 * @create 2024/11/19 11:59
 */
public class finallytest {

    public static void main(string[] args) {
        finallytest f = new finallytest();
        f.test1();
    }

    public void test1() {
        try {
            string str = "123";
            str = "abc";
            int i = integer.parseint(str);
            system.out.println(i);

        } catch (numberformatexception e) {
            e.printstacktrace();
            system.out.println(10 / 0);  //在catch中抛出异常
        } finally {
            system.out.println("程序执行结束");
        }
    }


    public void test2() {

        fileinputstream fis = null;
        try {
            file file = new file("d:\\hello.txt");
            fis = new fileinputstream(file);  //可能报filefonudexception

            int data = fis.read(); //可能报ioexception
            while (data != -1) {
                system.out.println((char) data);
                data = fis.read(); //可能报ioexception
            }

        } catch (filenotfoundexception e) {
            e.printstacktrace();
        } catch (ioexception e) {
            e.printstacktrace();
        } finally {
            try {
                if (fis != null) {
                    fis.close();  //可能报ioexception
                }

            } catch (ioexception e) {
                e.printstacktrace();
            }

        }
    }
}

二、异步处理的方式二:throws

1、格式

在方法的声明处,使用throws异常类型1,异常类型2,…

2、举例

public void test() throws 异常类型1,异常类型2,..{
	//可能存在编译时异常
}

3、是否真正处理了异常?

从编译是否能通过的角度看:看成是给出了异常万一要是出现时候的解决方案。此方案就是,继续向上抛出(throws)。
但是,此throws的方式,仅是将可能出现的异常抛给了此方法的调用者,此调用者仍然需要考虑如何处理相关异常。从这个角度来看,throws的方式不算真正意义上处理了异常。

import java.io.file;
import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.ioexception;

/**
 * package:package_name
 *
 * @author jimmy-yan
 * @create 2024/11/19 13:58
 */
public class throwstest {
    public static void main(string[] args) {
        test3();
    }

    public static void test3() {
        try{
            test2();
        }catch (filenotfoundexception e){
            e.printstacktrace();
        }catch (ioexception e){
            e.printstacktrace();
        }
    }

    public static void test2() throws filenotfoundexception, ioexception {

        file file = new file("d:\\hello.txt");
        fileinputstream fis = new fileinputstream(file);  //可能报filefonudexception

        int data = fis.read(); //可能报ioexception
        while (data != -1) {
            system.out.println((char) data);
            data = fis.read(); //可能报ioexception
        }
        fis.close();  //可能报ioexception
    }
}

4、方法重写的要求(只针对编译型异常)

子类重写的方法抛出的异常类型可以与父类被重写的方法抛出的异常类型相同,或者是父类被重写的方法抛出异常类型的子类。

5、开发中,如何选择异常处理的两种方式?

1、如果程序代码中,涉及到资源的调用(流、数据库连接、网络连接等),则必须考虑使用try-catch-finally来处理,保证不出现内存泄漏。
2、如果父类被重写的方法没有throws异常类型,则子类重写的方法中如果出现异常,只能考虑使用try-catch-finally进行处理,不能throws。
3、开发中,方法a中依次调用了方法b,c,d等方法,方法b,c,d之间是递进关系。此时,如果方法b,c,d中有异常,我们通常选择使用throws,而方法a中通常选择使用try-catch-finally。

三、使用throw手动抛出异常对象

1、为什么需要手动抛出异常

在实际开发中,如果出现不满足具体场景的代码问题,我们就有必要手动抛出一个指定类型的异常对象。

2、如何理解 自动vs手动 抛出异常

过程1:“抛”
“自动抛”:程序在执行的过程当中,一旦出现异常,就会在出现异常的代码处,自动生成对应异常类的对象,并将此对象抛出。
“手动抛”:程序在执行的过程当中,不满足指定条件的情况下,我们主动的使用"throw + 异常类的对象"方式抛出异常对象。

过程2:“抓”
狭义上讲:try-catch的方式捕获异常,并处理。
广义上讲:把“抓"理解为"处理"。则此时对应着异常处理的两种方式:

  • try-catch-fina
  • throws

3、如何实现手动抛出异常

在方法内部,满足指定条件的情况下,使用“throw 异常类的对象”的方式抛出。

/**
 * package:package_name
 *
 * @author jimmy-yan
 * @create 2024/11/19 14:51
 */
public class throw {
    public static void main(string[] args) {
        throw t = new throw();
        t.regist(-9);
        system.out.println(t);
    }

    int id;

    public void regist(int id) {
        if (id > 0) {
            this.id = id;
        } else {
//            system.out.println("输入的非法id");
            //todo 手动抛出异常
            throw new runtimeexception("输入的id非法");
        }
    }

    @override
    public string tostring() {
        return "throw{" +
                "id=" + id +
                '}';
    }
}

注意一点:throws后的代码不能被执行,编译不通过

总结

到此这篇关于java异常处理机制throws的文章就介绍到这了,更多相关java异常处理throws内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com