Java Thread线程内异常处理
2015-04-09 12:49
357 查看
本文章简单的记录一下异常相关的处理,已被后续遗忘后捡漏。
1.异常概念:
当发生某种 I/O 异常时,抛出此异常。此类是失败或中断的 I/O 操作生成的异常的通用类。
2.异常处理解决办法:
一、明确Java中的异常
在明确处理之前,先大体说说Java里面的异常。
1.异常概念:
Java中异常主要分为两大类:1).checked Exception(检查异常); 2).unchecked Exception(非检查异常)。对于unchecked exception也成RuntimeException. 那么,问题来了,何为检查呢? 简单的说,就是Java编译器是否要求你要捕获异常。对于unchecked exception,Java编译器不要求你一定要把它捕获或者一定要将抛出(throw),但对于checked exception,编译器会强制要求在method中捕获或者继续抛出的。 下面是jdk 1.6中对uncheck exception的 声明:
public class RuntimeException extends Exception
RuntimeException 是那些可能在 Java 虚拟机正常运行期间抛出的异常的超类。 可能在执行方法期间抛出但未被捕获的 RuntimeException 的任何子类都无需在 throws 子句中进行声明。 当然,对于check Exception, for instance: IOException :
public class IOException extends Exception
当发生某种 I/O 异常时,抛出此异常。此类是失败或中断的 I/O 操作生成的异常的通用类。
如上,我们可以看到,check exception直接extends Exception,是需要在编译的时候就通过throws继续抛出或者通过try-catch进行捕获的。 而uncheck exception(RuntimeException)是Exception的子类(如ArithmeticException extends RuntimeException),是不需要捕获或者继续抛出的。 此外,Error也应该归类为unchecked Exception,因为我们无法预知他们产生的时间。起因一般不是应用程序自身的调用,而是VM自身的问题。Error类一般标识应用程序无法解决的严重问题,故将其视为非检测异常。
2.异常处理解决办法:
1)对于unchecked Exception,我们一般有以下几种处理办法:1.捕获;2.继续抛出;3.不处理; 2)对于checked Exception,我们有以下几种处理方式:1.继续抛出,一致可以抛给java VM处理;2.用try...catchj进行捕获; 注意:对于检查异常必须处理或者捕获;
二、对于线程里非检测异常的处理
异常处理是Java语言的重要特性之一,主要帮助我们明确一下几个问题: 什么出错了? 那里出错了? 为什么出错? 而在Thread 的run方法中,假设对uncheck Exception未做出明确处理,但在运行时出现RuntimeException,那么这个Thread就有可能因一个Exception而终止,导致这个线程的终结。 而如果你想在没有做任何异常处理的情况下,对程序运行期间出现RuntimeException后做一定的资源释放或者后续工作处理,也就是说,在线程的run方法中出现一场后,要明确上面的三个问题。 那么,首先,你得有一个捕获handle这个消息的地方; 在jdk 5.0 之后,我们通过Thread的static方法 setUncaughtExceptionHandler() 静态方法设置未检查异常的异常处理器; 下来看一下,这个方法的声明定义:
/** * <p> * Sets the uncaught exception handler. This handler is invoked in case this * Thread dies due to an unhandled exception. * </p> * * @param handler * The handler to set or <code> null</code> . */ public void setUncaughtExceptionHandler(UncaughtExceptionHandler handler) { uncaughtHandler = handler; }
接下来,就简单了,就是实现UncaughtExceptionHandler接口的事了。
/** * Implemented by objects that want to handle cases where a thread is being * terminated by an uncaught exception. Upon such termination, the handler * is notified of the terminating thread and causal exception. If there is * no explicit handler set then the thread's group is the default handler. */ public static interface UncaughtExceptionHandler { /** * The thread is being terminated by an uncaught exception. Further * exceptions thrown in this method are prevent the remainder of the * method from executing, but are otherwise ignored. * * @param thread the thread that has an uncaught exception * @param ex the exception that was thrown */ void uncaughtException(Thread thread, Throwable ex); }
其有一个监听器 uncaughtException,thread:标识那个线程出现unchecked Exception;ex:标识异常类型。 举例:
package com.thread; public class ThreadUncatchTest { /** * @param args */ public static void main(String[] args) { Thread t = new Thread(new Runnable() { public void run() { int i = 2; int j = i / 0;//强制其抛出异常 System.out.println(j); /*try { } catch (Exception e) { System.out.println("--------------------"); e.printStackTrace(); }*/ } }); t.setUncaughtExceptionHandler(new MyThreadCatchExceptionHander()); t.start(); } } class MyThreadCatchExceptionHander implements Thread.UncaughtExceptionHandler{ public void uncaughtException(Thread thread, Throwable exception) { System.out.println("threadName:"+thread.getName()+";exception:"+exception.getMessage()); } }
上述例子演示了为某一特定的线程指定非检测异常的处理,也可以调用Thread.setDefaultUncaughtExcetpionHandler or Thread.setUncaughtExceptionHandler来全局设置线程的unchecked Exception捕获监听。
相关文章推荐
- Java并发编程示例(八):处理线程的非受检异常
- Thinking in Java学习笔记 Thread.UncaughtExceptionHandler接口实现捕获线程内异常
- java线程异常处理方法
- java线程异常的处理
- Java子线程中的异常处理
- Java thread中对异常的处理策略
- Java Thread线程异常监控
- Java thread中对异常的处理策略
- Java并发学习之八——在线程中处理不受控制的异常
- java线程对异常的处理
- JAVA线程池ScheduledExecutorService周期性地执行任务 与单个Thread周期性执行任务的异常处理
- Dubbo之——线程模型(Linux 用户线程数限制导致的 java.lang.OutOfMemoryError: unable to create new native thread 异常)
- java线程中异常的处理 -Java 7 Concurrency Cookbook 读书笔记
- android异常处理:线程退出(dvmDetachCurrentThread)检查未处理异常(dvmCheckException)-threadExitUncaughtException
- .NET异步多线程,Thread,ThreadPool,Task,Parallel,异常处理,线程取消
- 黑马程序员Java类之异常与线程(Thread)
- Java线程未捕获异常处理
- JAVA Thread线程异常监控
- Java线程未捕获异常处理
- 子线程中处理UI,抛异常 android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original