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

作者:一只小余 时间:2021-10-31 07:17:42 

异常方法

//返回此可抛出对象的详细信息消息字符串
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();
   }
}

来源:https://blog.csdn.net/qq_56717234/article/details/129466797

标签:java,自定义,异常,try-with-resources
0
投稿

猜你喜欢

  • Java内部类知识汇总

    2023-08-18 14:06:54
  • SpringMVC + servlet3.0 文件上传的配置和实现代码

    2023-08-08 16:42:43
  • java利用Future实现多线程执行与结果聚合实例代码

    2023-09-24 11:07:18
  • Spring Boot 读取静态资源文件的方法

    2023-08-25 02:53:07
  • C#实现ini文件读写操作

    2022-02-26 11:01:19
  • 详解SpringMVC验证框架Validation特殊用法

    2023-06-08 22:23:29
  • Springboot 使用maven release插件执行版本管理及打包操作

    2023-07-12 01:20:35
  • Java实现储存对象并按对象某属性排序的几种方法示例

    2022-05-04 18:05:57
  • java读取xml配置参数代码实例

    2023-11-25 03:03:17
  • Java 继承与多态的深入理解

    2023-10-05 04:25:41
  • 利用POI生成EXCEL文件的方法实例

    2023-11-23 21:44:14
  • JAVA设计模式----建造者模式详解

    2022-05-18 22:55:52
  • java基础-数组扩容详解

    2022-05-24 00:34:58
  • Spring MVC项目中log4J和AOP使用详解

    2022-11-16 08:36:29
  • MyBatis在注解上使用动态SQL方式(@select使用if)

    2023-09-24 06:52:55
  • Java中Controller、Service、Dao/Mapper层的区别与用法

    2022-09-12 15:14:03
  • Java JDK11基于嵌套的访问控制的实现

    2021-07-11 10:02:05
  • Java实现图片倒影的源码实例内容

    2022-08-30 02:39:24
  • Spring Security自定义认证逻辑实例详解

    2023-02-28 19:19:18
  • 使用Logback日志保存到相对路径的操作

    2021-10-11 16:42:30
  • asp之家 软件编程 m.aspxhome.com