1+ package com .javarush .test .level25 .lesson09 .task02 ;
2+
3+ import java .util .TimerTask ;
4+
5+ /* Вооружаемся до зубов!
6+ Создайте свой UncaughtExceptionHandler в виде локального класса внутри конструктора.
7+ UncaughtExceptionHandler должен маскать звездочками имя трэда.
8+ "Thread-0" должно быть заменено на "********"
9+ "Thread-4321" должно быть заменено на "***********"
10+ */
11+ public class Solution extends TimerTask {
12+ protected TimerTask original ;
13+ protected final Thread .UncaughtExceptionHandler handler ;
14+
15+ public Solution (TimerTask original ) {
16+ if (original == null ) {
17+ throw new NullPointerException ();
18+ }
19+ this .original = original ;
20+ this .handler = new Thread .UncaughtExceptionHandler () {
21+ @ Override
22+ public void uncaughtException (Thread t , Throwable e ) {
23+ System .out .println (e .getMessage ().replace (t .getName (), new String (new char [t .getName ().length ()]).replace ('\0' , '*' )));
24+ // System.out.println(new String(new char[e.getMessage().length()]).replace('\0', '*'));
25+
26+ }
27+
28+ }; //init handler here
29+ }
30+
31+ public void run () {
32+ try {
33+ original .run ();
34+ } catch (Throwable cause ) {
35+ Thread currentThread = Thread .currentThread ();
36+ handler .uncaughtException (currentThread , new Exception ("Blah " + currentThread .getName () + " blah-blah-blah" , cause ));
37+ }
38+ }
39+
40+ public long scheduledExecutionTime () {
41+ return original .scheduledExecutionTime ();
42+ }
43+
44+ public boolean cancel () {
45+ return original .cancel ();
46+ }
47+
48+ public static void main (String [] args )
49+ {
50+ Thread th = new Thread (new Solution (new TimerTask ()
51+ {
52+ @ Override
53+ public void run ()
54+ {
55+ throw new Error ();
56+ }
57+ }));
58+ th .start ();
59+ }
60+
61+ }
0 commit comments