KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > java > lang > RuntimeException

java.lang
Class RuntimeException

java.lang.Object
  extended by java.lang.Throwable
      extended by java.lang.Exception
          extended by java.lang.RuntimeException
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
AnnotationTypeMismatchException, ArithmeticException, ArrayStoreException, BufferOverflowException, BufferUnderflowException, CannotRedoException, CannotUndoException, ClassCastException, CMMException, ConcurrentModificationException, DOMException, EmptyStackException, EnumConstantNotPresentException, EventException, IllegalArgumentException, IllegalMonitorStateException, IllegalPathStateException, IllegalStateException, ImagingOpException, IncompleteAnnotationException, IndexOutOfBoundsException, JMRuntimeException, LSException, MalformedParameterizedTypeException, MissingResourceException, NegativeArraySizeException, NoSuchElementException, NullPointerException, ProfileDataException, ProviderException, RasterFormatException, RejectedExecutionException, SecurityException, SystemException, TypeNotPresentException, UndeclaredThrowableException, UnmodifiableSetException, UnsupportedOperationException
See Also:
Top Examples, Source Code

public RuntimeException()
See Also:
Throwable.initCause(java.lang.Throwable)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public RuntimeException(String message)
See Also:
Throwable.getMessage(), Throwable.initCause(java.lang.Throwable)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public RuntimeException(String message,
                        Throwable cause)
See Also:
Throwable.getCause(), Throwable.getMessage()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public RuntimeException(Throwable cause)
See Also:
Throwable.getCause()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1880]Writing Java signal handlers
By Anonymous on 2007/05/03 10:46:21  Rate
A little-known feature of Java is the ability of an application to install its own signal handler, which is supported through the sun.misc.Signal class. However, use caution when using classes from the sun.misc package because it contains undocumented support classes that may change between releases of Java. You can install a Java handler for any signal that is not used by the JVM. These signal handlers are similar to native handlers because they're invoked when a native system signal is raised, but they will always run as a separate Java thread. Essentially, when a signal is raised for which a Java signal handler is available, the JVM's "signal dispatcher thread" is woken up and informed of the signal. The signal dispatcher thread then invokes a Java method to create and start a new thread for the installed Java signal handler. 
  
  
 To write a Java signal handler, define a class that implements the sun.misc.SignalHandler interface and register the handler by using the sun.misc.Signal.handle (  )  method. The following example, which installs a Java signal handler for the SIGALRM signal, shows how it is done. The idea is to wrap another Java application, specified on the command line, so that whenever a SIGALRM signal is received a list of the current threads  ( for the default thread group )  will be output. It would be very easy to extend the signal handler to output additional information about the application or take some specific action, such as calling System.gc (  ) . 
  
  
 import sun.misc.Signal; 
 import sun.misc.SignalHandler; 
 import java.lang.reflect.*; 
  
  
 // Application Wrapper 
 // usage: java AppWrap  < app name >   < app arg1 >  ...  < app argn >  
 // where:  < app name >  is the name of the wrapped application class 
 // containing a main method 
 //         < app arg1 >  ...  < app argn >  are the application's arguments 
 class AppWrap  {  
     public static void main ( String [  ]  args )   {  
         try  {  
             // Install diagnostic signal handler 
             DiagSignalHandler.install ( "ALRM" ) ; 
              
             // Get the passed application's class 
             Class wrappedClass = Class.forName ( args [ 0 ]  ) ; 
              
             // Setup application's input arguments 
             String wrappedArgs [  ]  = new String [ args.length-1 ] ; 
             for  ( int i = 0; i  <  wrappedArgs.length; i++ )   {  
                 wrappedArgs [ i ]  = args [ i+1 ] ; 
              }  
              
             // Get the main method for the application 
             Class [  ]  argTypes = new Class [ 1 ] ; 
             argTypes [ 0 ]  = wrappedArgs.getClass (  ) ; 
             Method mainMethod = wrappedClass.getMethod ( "main", argTypes ) ; 
  
  
             // Invoke the application's main method 
             Object [  ]  argValues = new Object [ 1 ] ; 
             argValues [ 0 ]  = wrappedArgs; 
             mainMethod.invoke ( wrappedClass, argValues ) ; 
  
  
          }  catch  ( Exception e )   {  
             System.out.println ( "AppWrap exception "+e ) ; 
          }  
      }  
  }  
  
  
 // Diagnostic Signal Handler class definition 
 class DiagSignalHandler implements SignalHandler  {  
         
     private SignalHandler oldHandler; 
  
  
     // Static method to install the signal handler 
     public static DiagSignalHandler install ( String signalName )   {  
         Signal diagSignal = new Signal ( signalName ) ; 
         DiagSignalHandler diagHandler = new DiagSignalHandler (  ) ; 
         diagHandler.oldHandler = Signal.handle ( diagSignal,diagHandler ) ; 
         return diagHandler; 
      }  
  
  
     // Signal handler method 
     public void handle ( Signal sig )   {  
         System.out.println ( "Diagnostic Signal handler called for signal "+sig ) ; 
         try  {  
             // Output information for each thread 
             Thread [  ]  threadArray = new Thread [ Thread.activeCount (  )  ] ; 
             int numThreads = Thread.enumerate ( threadArray ) ; 
             System.out.println ( "Current threads:" ) ; 
             for  ( int i=0; i  <  numThreads; i++ )   {  
                 System.out.println ( " "+threadArray [ i ]  ) ; 
              }  
              
             // Chain back to previous handler, if one exists 
             if  (  oldHandler != SIG_DFL && oldHandler != SIG_IGN  )   {  
                 oldHandler.handle ( sig ) ; 
              }  
              
          }  catch  ( Exception e )   {  
             System.out.println ( "Signal handler failed, reason "+e ) ; 
          }  
      }  
  }  
  
  
 Because of the platform-specific nature of signals, this application will not run under Windows since it does not support the SIGALRM signal. But you could modify the application to use SIGINT, and start up with the -Xrs Java option. 
  
  
 An advantage of using Java signal handlers instead of native signal handlers is that your implementation can be completely in Java, keeping the application simple. Also, with Java signal handling you keep an object-oriented approach and refer to signals by name, making the code more readable than its C equivalent. There are many uses for Java signal handlers; you could create a simple interprocess communication between native and Java applications, and the signal could instruct the Java application to dump out useful diagnostics  ( as demonstrated by the above application )  or simply suspend/resume itself. 
  
  
 

Popular Tags