Exception handling code templates

WE Package; 

/ ** 
 * custom exception class 
 * @author JinXuLiang 
 * 
 * / 
class MyException the extends Exception 
{ 
    public MyException (the Message String) { 
        Super (the Message); 
    } 
    public MyException (String Message, the cause is the Throwable) { 
        Super (Message , the cause is); 
    } 
     public MyException (the cause is the Throwable) { 
        Super (the cause is); 
    } 

} 

public class ExceptionLinkInRealWorld { 
   public static void main (String args []) 
   { 
      the try { 
         throwExceptionMethod (); // may throw method invocation 
      } 
      catch ( MyException e )
      { 
         System.err.println (e.getMessage ()); 
         System.err.println (e.getCause () the getMessage ().); 
      } 
      The catch (Exception E) 
      { 
         System.err.println ( "main Exception in the Handled" ); 
      } 
      doesNotThrowException (); // not throw method calls 
   } 

   public static void throwExceptionMethod () throws MyException 
   { 
      
      the try { 
         System.out.println ( "method, throwException"); 

         initiated ( "system running throw new exception specific exception "); // creates a specific exception 
      } 
      the catch (exception E) 
      { 
         System.err.println ( 
            " in the Handled exception throwException Method "); 
         // convert a custom exception is thrown again
         throw new MyException("在方法执行时出现异常",e);

         
      }
      finally {
         System.err.println(
            "Finally executed in throwException" );
      }

      // any code here would not be reached
   }

   public static void doesNotThrowException()
   {
      try {
         System.out.println( "Method doesNotThrowException" );
      }
      catch( Exception e )
      {
         System.err.println( e.toString() );
      }
      finally {
         System.err.println(
            "Finally executed in doesNotThrowException" );
      }

      System.out.println(
         "End of method doesNotThrowException" );
   }
}

 

 

Guess you like

Origin www.cnblogs.com/yyl141/p/11789354.html