|
大家都知道在java开发中,我们可以用try catch来捕获异常,那么线程中出现了异常try catch 还依然管用吗?让我们一起先看个小例子吧。
" o3 v4 X" O; ] C! b6 v V" mpackage com.javazx.test.thread;
0 q% S3 [# n7 q; R. Y: d/ M, c2 }. w) M: E6 O7 O
import java.util.concurrent.ExecutorService;
. |$ h3 H2 {0 h! N+ n7 uimport java.util.concurrent.Executors;
7 _8 V+ q% t" g3 ]" ~; u
- v3 Y: t/ v7 Gpublic class ExceptionThread implements Runnable {
! t0 T' Z2 H& U: `. ^ public void run() {
2 z; z7 a( P% k; A, F# U# M throw new RuntimeException("线程内部出现异常");" s, o# q/ B1 g$ e) A, @/ ~ I
}
7 y& m+ A' s6 S3 f
* b; o9 u) {7 Y1 I public static void main(String[] args) {) v/ l/ A/ s# f# \; x3 ?- B
try {) e& `; [; `; o( q! x4 j
ExecutorService exec = Executors.newCachedThreadPool();
6 p& X. } O1 N( Q! g7 E+ x4 p4 Q exec.execute(new ExceptionThread());& s9 j( F; O" C
} catch (RuntimeException e) {
! j+ e% b+ ~, z System.out.println("能捕获到异常吗?");5 V7 T& h5 A2 s% U
}
3 y5 v1 J1 c$ C2 ^ }8 o8 }" R* B+ k, N. n/ g( b$ B
}
" B1 R# n% }- I0 q( t
/ W+ {0 B! P* }4 N/ }$ ]/ L1 _, ?7 h4 g0 g) X t# z
运行结果呢?+ n3 c* H: O/ M( ]
Exception in thread "pool-1-thread-1" java.lang.RuntimeException: 线程内部出现异常
# m* r3 Z9 _9 b- }0 a) ` ~2 r at com.javazx.test.thread.ExceptionThread.run(ExceptionThread.java:8)" s8 Q9 ~9 P. a8 r3 P# x6 ]$ R
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
* Z! z; {2 ]# n at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)) K$ h+ Q8 Y, p# P& L
at java.lang.Thread.run(Thread.java:748)" s$ s0 g9 G0 J' C2 A9 }
* `* M9 ^7 ~" u: r* ~7 V: B' n' Q
+ t* n( \/ o) h# ?' F答案已经很明显了,线程中是无法使用try catch来进行捕获的。那么如果要捕获异常呢 我们该怎么做呢?javase 1.5后我们是可以用UncaughtExceptionHandler来捕获异常的,具体要怎么做呢?大家还是看demo吧。+ Q x9 h: {) ]' g1 ~5 i# _2 |
1、
$ {& H8 _( G; h. t1 Rpackage com.javazx.test.thread; i* k8 `; M$ b4 j
' [2 q! T5 U! L9 X, S6 u' V6 a* X/ `public class ExceptionThread2 implements Runnable {
2 [3 _" v+ y' q9 i6 _; Z5 O% v
; s7 _/ T6 q0 n+ D public void run() {
4 K0 o1 u4 \ y, I2 r# s throw new RuntimeException("抛出运行时异常");
. [5 I) F! P& _8 } }+ @. X) @" ] y p( W9 w, u& ^# L+ |
}
$ y/ O$ h2 L, S; O/ p! h$ D5 z& \. k
2、 @( @" f* q% J% ^5 S
package com.javazx.test.thread;
; F8 C ^& J9 Q# c
. h& @( m, K" [2 m# Dpublic class MyUnchecckedExceptionhandler implements Thread.UncaughtExceptionHandler {
7 Y! ]/ d# n# S& `% @ public void uncaughtException(Thread t, Throwable e) {
( z: e/ S: y& L3 Z- Z/ [/ t System.out.println("捕获到异常:" + e);, Q5 N. q6 v. D; y+ M2 N
}3 w7 o5 @* ?# V% j$ z# m) P" S
public static void main(String [] args){
C% E" Z% S/ M Thread t=new Thread(new ExceptionThread2());0 U; Z& n, t: U( P: X* V
t.setUncaughtExceptionHandler(new MyUnchecckedExceptionhandler());
! ~0 y" y$ M8 i- G$ C3 x. t! m5 Y t.start(); r0 e% f! R- H" Y! \7 P
}
+ q' ^( D# i" r% p0 @) X; E, v6 x* G}' b$ x. b* z" k) s/ I' B: v
* Q' U. s& Q- p: \' d5 z( c& U, b' z! q6 M
+ e$ c3 o7 a4 x# w2 e# y运行结果:5 u6 I. O; f2 i# R$ N+ U
捕获到异常:java.lang.RuntimeException: 抛出运行时异常
% S' J( j% G l+ T5 a, v' v
9 W- K. L6 e3 Q$ g A+ Z4 E
% o) E7 o: R% O/ C8 @终于捕获到了异常,大功告成!
0 V6 z# U& s2 m Q# G }- d
/ i6 o. a! D+ u& p7 K/ W, c% O
! I& v2 e+ d& }( Q+ H3 W# W: G
: d- E- [( I3 k& q# r" P% { |
|