Thinking in java4 Exception
来源:互联网 发布:举报网络电子邮箱地址 编辑:程序博客网 时间:2024/06/11 18:08
Thinking in java4 Exception
一 创建自定义异常 异常和日志
LoggingExceptions:
LoggingExceptions2:
打印日志
StringWriter trace = new StringWriter();printStackTrace(new PrintWriter(trace));logger.severe(trace.toString());ExtraFeatures: e.printStackTrace(System.out) ,e.printStackTrace(System.err) ==e.printStackTrace()
一 捕获异常,异常打印源码
ExceptionMethods:
e.getMessage() ==e.getLocalizedMessage()
e = e.getClass().getName() + e.getLocalizedMessage()
e.printStackTrace(System.out) printStackTrace 获取堆栈信息 StackTraceElement。
WhoCalled:
记录堆栈的类,方法,行,可以看看StackTraceElement.toString()就是我们的异常信息,e.getStackTrace()获取堆栈信息
Rethrowing:
去除之前的异常堆栈,从当前点打印异常堆栈, throw (Exception) e.fillInStackTrace();
一 不用抛出异常RuntimeException
NeverCaught:不用抛出异常
//: exceptions/NeverCaught.java// Ignoring RuntimeExceptions.// {ThrowsException}public class NeverCaught { static void f() { throw new RuntimeException("From f()"); } static void g() { f(); } public static void main(String[] args) { g(); }} ///:~
一 异常丢失
LostMessage:finally抛出异常,导致之前的异常丢失。
ExceptionSilencer
//: exceptions/ExceptionSilencer.javapublic class ExceptionSilencer { public static void main(String[] args) { try { throw new RuntimeException(); } finally { // Using 'return' inside the finally block // will silence any thrown exception. return; } }} ///:~
一 异常重载
StormyInning : 抛出子类最好,先处理子类异常,在处理基类异常。
一 构造函数抛出异常,try 嵌套
Cleanup: 两个try捕获
CleanupIdiom
//: exceptions/CleanupIdiom.java// Each disposable object must be followed by a try-finallyclass NeedsCleanup { // Construction can't fail private static long counter = 1; private final long id = counter++; public void dispose() { System.out.println("NeedsCleanup " + id + " disposed"); }}class ConstructionException extends Exception {}class NeedsCleanup2 extends NeedsCleanup { // Construction can fail: public NeedsCleanup2() throws ConstructionException {}}public class CleanupIdiom { public static void main(String[] args) { // Section 1: NeedsCleanup nc1 = new NeedsCleanup(); try { // ... } finally { nc1.dispose(); } // Section 2: // If construction cannot fail you can group objects: NeedsCleanup nc2 = new NeedsCleanup(); NeedsCleanup nc3 = new NeedsCleanup(); try { // ... } finally { nc3.dispose(); // Reverse order of construction nc2.dispose(); } // Section 3: // If construction can fail you must guard each one: try { NeedsCleanup2 nc4 = new NeedsCleanup2(); try { NeedsCleanup2 nc5 = new NeedsCleanup2(); try { // ... } finally { nc5.dispose(); } } catch(ConstructionException e) { // nc5 constructor System.out.println(e); } finally { nc4.dispose(); } } catch(ConstructionException e) { // nc4 constructor System.out.println(e); } }} /* Output:NeedsCleanup 1 disposedNeedsCleanup 3 disposedNeedsCleanup 2 disposedNeedsCleanup 5 disposedNeedsCleanup 4 disposed*///:~
0 0
- Thinking in java4 Exception
- Thinking in java4 容器
- Thinking in java4 15章 泛型
- Thinking in java4 16章 数组
- java线程 同步临界区:thinking in java4 21.3.5
- Thinking in java4 14章 RTT和反射学习笔记
- Thinking in Exception(Base on Alfresco)
- Thinking in java-34 Exception异常处理
- java线程共享受限资源 解决资源竞争 thinking in java4 21.3
- java 并发原子性与易变性 来自thinking in java4 21.3.3
- java 线程 原子类相关操作示例 thinking in java4 目录21.3.4
- java线程 在其他对象上同步、线程本地存储ThreadLocal:thinking in java4 21.3.6
- java 线程 在阻塞时终结 之中断讲解 ---thinking in java4
- Thinking in java中关于Exception的一道面试题.
- java4
- Java4
- java4
- java4
- C#多线程同步(一)【计数器】
- mybatis 报错 Specified class is an interface
- 异常继承
- 【逻辑思考】我们要有把坏事看成好事的能力
- nginx搭建支持http和rtmp协议的流媒体服务器之二 【转】
- Thinking in java4 Exception
- 【OpenCV入门教程之八】线性邻域滤波专场:方框滤波、均值滤波与高斯滤波
- c++网络多人五子棋
- 在树莓派上使用Python实现对温度,湿度以及光照强度的实施测量
- 第十二章 迭进
- SDK4/5/6/7,相册、拍照及裁剪功能及遇见的坑
- nginx搭建支持http和rtmp协议的流媒体服务器之三 【转】
- 通讯录(链表+文件实现)+ 反思
- MySQL5.7版本无法启动服务【启动后自动停止】的解决方法