|
大家都知道在java开发中,我们可以用try catch来捕获异常,那么线程中出现了异常try catch 还依然管用吗?让我们一起先看个小例子吧。 V) c2 U' @+ z9 W8 p+ V2 g
package com.javazx.test.thread;8 i* E% ^5 f' W! s# x* R' C3 y
8 [- |6 O# E' J: U# H
import java.util.concurrent.ExecutorService;
' o2 B" j [* }( @import java.util.concurrent.Executors;
: u6 F. ~, g+ G u8 c/ T
% L. B! d: \- b9 p( {% l1 upublic class ExceptionThread implements Runnable {
C# h Q, U* P$ k! k9 H public void run() {
1 L6 u+ p5 c" Z6 N throw new RuntimeException("线程内部出现异常");$ o9 Q; h' _. c0 a- G2 L" S
}
# Q& ^3 b# `4 b. F* N/ S# t \0 y: s; B' ]
public static void main(String[] args) {! A+ X0 `1 M( @0 ` R% T+ B
try {
* A1 |0 r8 |. D/ j, [8 ]; V8 O ExecutorService exec = Executors.newCachedThreadPool();
# K2 {, ?" |7 \/ _ exec.execute(new ExceptionThread());3 W$ C' S7 Y" l/ K* w9 F' p
} catch (RuntimeException e) {4 H* r1 f) v4 x1 d
System.out.println("能捕获到异常吗?");
0 @' e: E. d* Z0 s8 Z% Y, X }
2 R3 d3 V1 s% g4 Z: K } j7 b. C: ?: W; ]2 C6 D
}& X0 U$ f) o& G0 w8 E9 T+ q2 D& u7 F
' V, s6 C. E; b/ k! a+ P
; S3 z8 O% t$ b# }# y运行结果呢?
# h- G- |3 M) S- OException in thread "pool-1-thread-1" java.lang.RuntimeException: 线程内部出现异常
" h' C$ r6 d2 A' K2 d) Z at com.javazx.test.thread.ExceptionThread.run(ExceptionThread.java:8)
. Z7 r8 l: s" r- C: D at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)' i* H' K5 I [; i3 P* t3 B. O8 M( ~- |
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
0 X U; e( E/ D3 y% a! E at java.lang.Thread.run(Thread.java:748)* w; N3 s R' k) b" z+ e H$ F
: V2 k! z0 N! `7 h( b: i+ \( g+ |; O
. u" g+ V& K* X0 c答案已经很明显了,线程中是无法使用try catch来进行捕获的。那么如果要捕获异常呢 我们该怎么做呢?javase 1.5后我们是可以用UncaughtExceptionHandler来捕获异常的,具体要怎么做呢?大家还是看demo吧。$ ]- B) j+ x# x& p8 M' Y, {
1、2 a/ ]$ M2 ~# p8 B$ m& ?
package com.javazx.test.thread;
1 g( _0 q" Z* J2 H! m( f
+ }' q3 S9 e( E2 Xpublic class ExceptionThread2 implements Runnable {
6 K0 B# O. |. G. e: i2 |
: m) D5 O% M1 E, f, h$ \0 Z/ `- v/ {' H& } public void run() {
' S1 c; G, v5 B3 M5 W0 ^ throw new RuntimeException("抛出运行时异常");8 G9 E4 R* c" J4 F- e
}
2 ]8 e. O n3 o" V. A}
- N6 z2 z; |0 }& b' s+ p
\2 o& a4 m4 \- |2、1 y1 t( Z' D6 Z! G0 o% \
package com.javazx.test.thread;9 Q9 O! [* F# \4 p I' q$ ?+ |
4 x6 Z Q- l6 O: p
public class MyUnchecckedExceptionhandler implements Thread.UncaughtExceptionHandler { B3 |" A+ J: m6 L( n. ~6 j1 k8 ]; B
public void uncaughtException(Thread t, Throwable e) {
C5 S$ j/ t) j$ b$ [* T System.out.println("捕获到异常:" + e);
% a# N* q4 F! H# l }. G, a1 b% |" m- p
public static void main(String [] args){, W0 _8 g% x+ g a ]/ j
Thread t=new Thread(new ExceptionThread2());
9 y1 G9 R' D! e7 o% ? t.setUncaughtExceptionHandler(new MyUnchecckedExceptionhandler());/ ?/ {* Y4 D% u, V2 R0 N
t.start();; o& j- U) Y4 J W$ G' O* ?$ ~; J) a
}
" [" y0 x9 u; f; W6 W}# V4 F A% |4 i( s
- @* M2 S1 J3 q4 r' K2 h
& j1 o. `# N* W h! _7 T+ M8 }' j- }$ n' J3 X
运行结果:! Q; }: z! {2 ]+ S% t
捕获到异常:java.lang.RuntimeException: 抛出运行时异常
' e( |5 W5 U6 q8 D$ {- ~& G" t" E$ G) D8 u7 G! b0 Y
, L! q# i. Y& w, N( }
终于捕获到了异常,大功告成!, o1 H% d4 K4 O
5 z5 |. a2 |3 C9 I( |- `/ r7 s5 A
! ]0 h6 l6 C$ g& i
; f5 c" f% `3 v+ c |
|