广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Java异常--常见方法--自定义异常--增强try(try-with-resources)详解
  • 462
分享到

Java异常--常见方法--自定义异常--增强try(try-with-resources)详解

java自定义异常java增强tryjavatry-with-resources 2023-03-14 11:03:42 462人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

目录异常方法自定义异常作用定义增强try(try-with-resources)作用条件jdk7jdk9异常方法 //返回此可抛出对象的详细信息消息字符串 public String

异常方法

//返回此可抛出对象的详细信息消息字符串
public String getMessage() 

//将此可抛发对象及其回溯到标准错误流。此方法在错误输出流上打印此 Throwable 对象的堆栈跟踪
//最为详细
public void printStackTrace()
//返回此可抛件的简短说明
public String toString()

对于1/0这个异常

 try{
            int i = 1/0;
        } catch(Exception e){
            System.out.println("e = " + e);
            System.out.println("-----------------");
            System.out.println("e.getMessage() = " + e.getMessage());
            System.out.println("-----------------");
            System.out.println("e.getStackTrace() = " + Arrays.toString(e.getStackTrace()));
            System.out.println("-----------------");
            System.out.println("e.getLocalizedMessage() = " + e.getLocalizedMessage());
            System.out.println("-----------------");
            System.out.println("e.getCause() = " + e.getCause());
            System.out.println("-----------------");
            System.out.println("e.getClass() = " + e.getClass());
            System.out.println("-----------------");
            System.out.println("e.getSuppressed() = " + Arrays.toString(e.getSuppressed()));

        }
e = java.lang.ArithmeticException: / by zero
-----------------
e.getMessage() = / by zero
-----------------
e.getStackTrace() = [省略27行,com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)]
-----------------
//可能的原因
e.getCause() = null
-----------------
//一个数组,其中包含为传递此异常而禁止显示的所有异常。
//就是用try捕获却不做事的
e.getSuppressed() = []

自定义异常

作用

让控制台的报错信息更加的见名知意

定义

1.定义异常类,写继承关系。
名字要见名知义,继承于异常类。
像运行时可以继承RuntimeException
开发过程中一般会有多种异常类,小的会继承自定义的大的。

2.写构造方法
需要书写空参和带参的构造。
可以调用父类的也可以自定义

增强try(try-with-resources)

作用

简化释放资源的步骤

条件

自动释放的类需要实现autocloseable的接口
这样在特定情况下会自动释放,还有的就是stream流中提到过。

jdk7

try(创建对象资源1;创建对象资源2){

}catch(){
}

例如这样的代码可以改写成

BufferedInputStream b = null;
try {
    b = new BufferedInputStream(new FileInputStream(""));
}catch (Exception e) {
    e.printStackTrace();
}finally {
    if (b!=null) {
        try {
            b.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
try (BufferedInputStream b = new BufferedInputStream(new FileInputStream(""));){
    
}catch (Exception e) {
    e.printStackTrace();
}

jdk9

创建对象1
创建对象2
try(变量名1;变量名2){
}catch(){
}

上面的代码可以改写成,
不过需要注意的是创建对象也需要异常处理,我们这里选择抛出

public void testTryWithResource() throws FileNotFoundException {
    BufferedInputStream b = new BufferedInputStream(new FileInputStream(""));
    try (b) {

    } catch (Exception e) {
        e.printStackTrace();
    }
}

到此这篇关于java-异常--常见方法--自定义异常--增强try(try-with-resources)的文章就介绍到这了,更多相关java自定义异常内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Java异常--常见方法--自定义异常--增强try(try-with-resources)详解

本文链接: https://www.lsjlt.com/news/199708.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作