|
大家都知道在java开发中,我们可以用try catch来捕获异常,那么线程中出现了异常try catch 还依然管用吗?让我们一起先看个小例子吧。/ k( m- R/ P" @& K9 C) F
package com.javazx.test.thread;
0 g; Q5 P2 o( l$ F" C: _% o
7 q5 a5 m, [& Timport java.util.concurrent.ExecutorService;. c9 q- K3 E7 Y8 W! n3 i: c1 d$ j! V
import java.util.concurrent.Executors;0 \! i' G. k" c& X- E
0 E5 [# ^1 o/ d6 C* J( D+ fpublic class ExceptionThread implements Runnable {
4 M3 H( R. C. U. X1 |$ _4 R public void run() {
% j' i1 h0 P( Q/ Y0 y0 c5 ~/ u throw new RuntimeException("线程内部出现异常");4 q" v& y/ Y" F) y: Z
}
6 X `0 S) s# O) C; c" ?5 |3 _0 `7 m( ]! F$ x/ z E
public static void main(String[] args) {
9 { @7 g1 V+ r" r try {
- H4 L6 s. T" L/ s u5 x3 {1 _ ExecutorService exec = Executors.newCachedThreadPool();5 ?/ W- K! N8 u) C, Z3 b
exec.execute(new ExceptionThread());
0 {' o( d) H3 Y' d } catch (RuntimeException e) {
+ v. ^2 n6 b( ~ System.out.println("能捕获到异常吗?");; a( \ u0 d9 O/ C1 O, g* j
}
6 n ~5 T }$ k* p6 c8 p }
: b) o+ n8 u- f: I3 m: X}# _* U- K7 N: T! e6 n' R) r! ^
8 A+ H& ~$ N0 B3 j, i6 i1 @1 E2 q
3 Y9 _3 R) I& h* S6 v" p运行结果呢?- D+ c2 c9 v1 L& r% J9 N$ l7 V
Exception in thread "pool-1-thread-1" java.lang.RuntimeException: 线程内部出现异常
; j" a0 P* r2 x. i. v: R2 k1 ? at com.javazx.test.thread.ExceptionThread.run(ExceptionThread.java:8)
. C* ]* ^/ ^& U( ] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)4 ~7 t* i+ h: D; ]8 I/ f( D
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)8 B" O; c* j& u, B: x0 V
at java.lang.Thread.run(Thread.java:748)) M \( k* q5 `4 g K
" J! k' \$ J8 y1 v$ F# [% Z! [. N$ K$ N1 k# w) I, H5 L
答案已经很明显了,线程中是无法使用try catch来进行捕获的。那么如果要捕获异常呢 我们该怎么做呢?javase 1.5后我们是可以用UncaughtExceptionHandler来捕获异常的,具体要怎么做呢?大家还是看demo吧。
# r3 z. p, _! t1、
, Z$ s& F8 X c* C# Dpackage com.javazx.test.thread;
" {3 Q& j& e6 {
! [# w% N8 b) [' c; l% {' opublic class ExceptionThread2 implements Runnable {
! w4 o# \1 p" b8 l9 Q; M/ L0 g" o# x7 d/ {2 n0 n
public void run() {
# `: w; `+ ~% h0 F throw new RuntimeException("抛出运行时异常");) o* R! T9 q+ Y3 R, B o; ~& `! ]
}
; R" i; o) D- H4 z0 ^& Q# f7 N V}
, X; n/ k* B! C1 y7 h! ]. {
3 M, b! k* A( ?) L! u7 [* j2、
/ d, B2 @; g3 ^! c4 j# T7 apackage com.javazx.test.thread;
2 Y( s/ e* R. y9 }; ?& k9 s/ q
3 ^9 @* Q, W$ |" Apublic class MyUnchecckedExceptionhandler implements Thread.UncaughtExceptionHandler {9 T) v4 f' y' P+ c
public void uncaughtException(Thread t, Throwable e) {
2 {5 c: a$ G. \5 q& | System.out.println("捕获到异常:" + e);
+ r7 A4 L! S5 R3 ]# N }
& _* p5 A7 A- v. z public static void main(String [] args){
/ d; X. A, [, F% a& Y# u Thread t=new Thread(new ExceptionThread2());
0 D' U! n$ a; Y6 l8 ^ X t.setUncaughtExceptionHandler(new MyUnchecckedExceptionhandler());
% C8 m. z6 ?! v) x2 @: Y9 `& {- e; ] t.start();
. H4 Z7 H8 ?8 g1 e3 f$ R3 i }
+ y4 F' @% z( S9 V& t" N/ T/ o% n# |1 `/ `}/ |9 _8 I4 m8 _8 |- @1 e& h: x3 W
2 v# z' R; F ]7 E# l3 W d G: E
$ F5 g7 e8 x: `
3 O# n& Z p7 a: `
运行结果:
, S5 C( f/ Y& `- Q! h5 y5 _捕获到异常:java.lang.RuntimeException: 抛出运行时异常$ q! z, W: Y8 X% k
# R; ?' w* ]8 R% u
; T% \: r8 z- M( T
终于捕获到了异常,大功告成!
" a" Q) e- t. a1 N+ e8 v# [3 [, z. Q( A
5 S: ^% y+ F% s5 o
( u; H3 z& z- s9 B( N
|
|