|
大家都知道在java开发中,我们可以用try catch来捕获异常,那么线程中出现了异常try catch 还依然管用吗?让我们一起先看个小例子吧。
3 \) k1 z9 @- j; ^0 ppackage com.javazx.test.thread;
. G2 `5 U* F1 H: p: D
' k6 Q! \5 ]- r/ W0 |, Timport java.util.concurrent.ExecutorService;- }" m8 A9 `$ m* _2 x, y% a4 H
import java.util.concurrent.Executors;( L* Y9 Z0 n$ }2 y
1 ?- r0 r# h/ W# l
public class ExceptionThread implements Runnable {
2 }. h6 u+ u1 q" W public void run() {
, t$ f: A: u: i5 T4 H# `, P! [8 g throw new RuntimeException("线程内部出现异常");
; D8 ~$ s+ G* e* B6 W% r }9 ? r t7 h2 A
7 t% ?. j1 {/ V) d5 F7 d
public static void main(String[] args) {3 s! H* q! j+ [$ m" P) s: ~
try {
- @6 r% ?4 m# G' u8 m7 d% @) E ExecutorService exec = Executors.newCachedThreadPool();
9 ~0 `) ~2 P* K exec.execute(new ExceptionThread());
$ k1 W! R& _6 B Q5 Q } catch (RuntimeException e) {$ t: N0 y% [+ g( |- L
System.out.println("能捕获到异常吗?");
, J' b% Z# a" F6 } }
. C7 ?2 G+ e% C2 e }
5 ?; L" ^5 A: k1 p# C2 E7 z+ m}
# x; c6 f) h7 u v
/ |0 R$ `$ q( c% B+ M9 M% b1 ^/ u5 K2 F" p: `, i( {5 |6 c g" N
运行结果呢?
% \2 L# U9 S; x& qException in thread "pool-1-thread-1" java.lang.RuntimeException: 线程内部出现异常
4 y9 ^* S/ L8 G6 { at com.javazx.test.thread.ExceptionThread.run(ExceptionThread.java:8)9 Q0 I I! u" c7 N# ?# t, F! W/ [
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
5 j3 {; H& D1 u at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)9 {7 z) P: t; \" R6 ^! S- Z1 X
at java.lang.Thread.run(Thread.java:748)
* `( m I( k" j% f7 x! d+ b! r' a* J
! `5 w+ ~: ?: O& I5 D
答案已经很明显了,线程中是无法使用try catch来进行捕获的。那么如果要捕获异常呢 我们该怎么做呢?javase 1.5后我们是可以用UncaughtExceptionHandler来捕获异常的,具体要怎么做呢?大家还是看demo吧。! L$ c& i4 y5 Q4 d6 m! ^* h! D9 Z
1、
$ _! _( {( P( O: e# m7 }$ B" Q( Ipackage com.javazx.test.thread;. U1 i; J8 P+ P n! w
, @ C- X3 p( h: o, i( |
public class ExceptionThread2 implements Runnable {
+ _) c* t5 U# i% [- [8 p$ Z% t) P5 J, q/ y- A
public void run() {
: A( K) F q ^/ l* P throw new RuntimeException("抛出运行时异常");# ^$ g. |6 [6 D Q! B! t
}
. k" c0 P0 P% E% L}
' Z% ]& v" |; i7 ~4 h& s' h2 G% d
! `: \. Z8 Y' b* V' Z4 }2、% u, C. L+ h" r% C9 o% {1 T8 Z1 B
package com.javazx.test.thread;" {# t) O' Q1 t$ Z
1 a7 N( x2 B" b0 G+ i% l) f
public class MyUnchecckedExceptionhandler implements Thread.UncaughtExceptionHandler {
% I, F, x+ Y |9 y5 _ public void uncaughtException(Thread t, Throwable e) {0 g6 W1 N+ o6 H; @% _1 h, ?
System.out.println("捕获到异常:" + e);
" C) a; V6 K2 M% Q1 X }- G! C, n2 Z- q+ s
public static void main(String [] args){) B$ D# b' a; Z4 _2 ~# T6 A2 i' y: a* w
Thread t=new Thread(new ExceptionThread2());
2 R4 U& W7 B# Q8 y6 } t.setUncaughtExceptionHandler(new MyUnchecckedExceptionhandler());
, o# H7 }/ z1 z' L' y/ _ t.start();/ z7 b2 g2 S( F' p R& k, {
}0 M9 c# j5 u7 A1 M. J6 G
}
. r4 X V# d, R9 [. z3 i/ k
1 T3 f& W- j( ]3 n7 Z) O" r! h
& v5 N6 V2 w1 C4 v6 }4 {6 h! \! {+ R6 a, _) b
运行结果:
! o! h$ N; C1 l/ l- [( _1 Y捕获到异常:java.lang.RuntimeException: 抛出运行时异常
; D6 U9 x0 Z, r- y+ y9 p
. V7 m9 b, |) S$ t5 A
8 P, N1 U2 b: T6 y4 v终于捕获到了异常,大功告成!9 k) A' a/ E4 O5 C
, X; K) ~3 e7 }6 _7 Z1 r
2 J! V8 ~$ H/ o& n* E
+ P5 y6 {4 t, Q) k# s3 ^ |
|