|
大家都知道在java开发中,我们可以用try catch来捕获异常,那么线程中出现了异常try catch 还依然管用吗?让我们一起先看个小例子吧。) r7 b \8 {% u( [; |
package com.javazx.test.thread;
! d' M# `5 |6 s0 ^9 O5 ~% S7 a. v0 E% ?* B( N
import java.util.concurrent.ExecutorService;
- @! [) Q9 I: O& n( ^3 _import java.util.concurrent.Executors;
/ c8 U; |4 e: a/ U3 [& W) {8 \9 Q, f5 v" M8 R
public class ExceptionThread implements Runnable {% y0 W$ n Z2 r, V; E k
public void run() {$ l+ \5 ]% I3 _ J9 n
throw new RuntimeException("线程内部出现异常");# N7 B6 g5 X6 T4 r
}
: n2 _0 m; ?3 @& f& _
! ]# z# v8 g( g6 U5 ` public static void main(String[] args) {+ f2 w' x: t2 k8 R! _+ u1 f
try {
+ O7 e6 M$ R' Q1 Z9 q! B$ k% T ExecutorService exec = Executors.newCachedThreadPool();( I. I+ P F& d" h/ U4 h
exec.execute(new ExceptionThread());
' ]1 O: E7 w& r } catch (RuntimeException e) {0 Z# x2 _2 H8 O8 e3 N( b. O3 P
System.out.println("能捕获到异常吗?");" t m7 \8 e* }2 l3 b
}
3 C+ F# N; E) o# B; E* k& G }. Y4 M6 K: s6 B
}
7 @$ R1 `; T+ K8 |+ C0 e8 f) C5 T2 y
* H4 P y/ e& P" O6 o8 f0 l
运行结果呢?: }4 H+ E$ k- O/ Z* J1 u u4 ^3 a
Exception in thread "pool-1-thread-1" java.lang.RuntimeException: 线程内部出现异常
0 m6 I" V p1 X b at com.javazx.test.thread.ExceptionThread.run(ExceptionThread.java:8)
M2 S+ ?1 j6 r+ r! L7 x# S* f at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
; a! \- h) o& K5 ^ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) C( x5 ?4 b/ A) u/ B# ]/ \$ k
at java.lang.Thread.run(Thread.java:748)2 N3 k1 D2 x- K1 X) y
; p# K6 E) p& E3 A6 Z, ?
2 ^6 w6 T- U. m( @7 J5 U/ I答案已经很明显了,线程中是无法使用try catch来进行捕获的。那么如果要捕获异常呢 我们该怎么做呢?javase 1.5后我们是可以用UncaughtExceptionHandler来捕获异常的,具体要怎么做呢?大家还是看demo吧。 T }' V; M8 F c
1、
& B' R$ f; _3 ?# r, _package com.javazx.test.thread;: [: [2 l- v0 }6 n
. k( r1 l2 L* ~9 y% d1 L. \1 {public class ExceptionThread2 implements Runnable {
3 Q8 H: E6 l, B5 L8 k( x) l3 _2 Q$ x1 D
public void run() {
! ^7 b- ^% V7 t3 N4 T& B6 m throw new RuntimeException("抛出运行时异常");
# y; ?, x: {; C/ R' X }
6 b, b0 d4 `9 k( e/ y}! h( N1 V) @( [% p2 l+ g* w' N
) T' g" U" j6 `3 O2、+ `9 L' V+ W- ~. ~1 F
package com.javazx.test.thread;) r, U' H6 h, A9 d6 [
% V- L6 F6 f8 V' M8 S; X, X5 Y9 qpublic class MyUnchecckedExceptionhandler implements Thread.UncaughtExceptionHandler {, \7 y0 W" \% M3 B2 H
public void uncaughtException(Thread t, Throwable e) {3 c- G" D7 e0 M$ `; u; |) Z' x* M9 x
System.out.println("捕获到异常:" + e);
, d7 d% l: l9 v4 a8 o- A) X }
+ j! b9 K" H( ^ z; I& D public static void main(String [] args){. i: U3 l8 ]1 G! R. @! V
Thread t=new Thread(new ExceptionThread2());
" ~! u! L8 w1 Q3 q* b% s t.setUncaughtExceptionHandler(new MyUnchecckedExceptionhandler());$ E& W+ H6 Z% p: K
t.start();& `" I# g# B) A" G. {5 U
}
2 m+ e' X2 c3 [}4 q* a( p: A' M
1 { x& a6 q- q, |
9 \; L6 E) J( o- T& r0 P' O5 y o+ q
运行结果:! I4 s. l- S& |7 c0 G' k( w
捕获到异常:java.lang.RuntimeException: 抛出运行时异常
1 J% c8 `- t" W& E' K" e8 }$ x) P( J% u) B) A1 {' E: o' V
4 W$ a i) N6 w! o/ g
终于捕获到了异常,大功告成!7 \& R4 i9 Y9 N/ [/ D0 \1 K+ ]' L
: B, C" S$ n; Z' n. u1 b5 z; Y$ }& V; m) `0 S
+ o& p; G6 n$ Y9 O |
|