1 /**
2  * The utillib library.
3  * More information is available at http://www.jinchess.com/.
4  * Copyright (C) 2002 Alexander Maryanovsky.
5  * All rights reserved.
6  *
7  * The utillib library is free software; you can redistribute
8  * it and/or modify it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2 of the
10  * License, or (at your option) any later version.
11  *
12  * The utillib library is distributed in the hope that it will
13  * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with utillib library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21 
22 package free.util;
23 
24 /**
25  * A runnable which catches and prints all Throwables except ThreadDeath
26  * thrown within its run method. This should be used by code invoking some user
27  * code to prevent the calling thread from dying because some exception was thrown
28  * in the user code.
29  */
30 
31 public abstract class SafeRunnable implements Runnable{
32 
33 
34   /**
35    * Wraps a call to safeRun() within a try..catch block which catches and
36    * prints all RuntimeExceptions and Errors (except ThreadDeath) to the standard
37    * error stream.
38    */
39 
run()40   public final void run(){
41     try{
42       safeRun();
43     } catch (Throwable t){
44         if (t instanceof ThreadDeath)
45           throw (ThreadDeath)t;
46 
47         System.err.println("An exception/error occurred while handling data:");
48         t.printStackTrace();
49       }
50   }
51 
52 
53 
54   /**
55    * The method that should be implemented instead of the run() method.
56    */
57 
safeRun()58   public abstract void safeRun();
59 
60 }
61