usingexceptions.java

来自「金旭亮的java教案」· Java 代码 · 共 60 行

JAVA
60
字号
//  UsingExceptions.java
// Demonstration of the try-catch-finally
// exception handling mechanism.
public class UsingExceptions {
   public static void main( String args[] )
   {
      try {
         throwException();
      }
      catch ( Exception e )
      {
         System.err.println( "Exception handled in main" );
      }

      doesNotThrowException();
   }

   public static void throwException() throws Exception
   {
      // Throw an exception and immediately catch it.
      try {
         System.out.println( "Method throwException" );
         throw new Exception();  // generate exception
      }
      catch( Exception e )
      {
         System.err.println(
            "Exception handled in method throwException" );
         throw e;  // rethrow e for further processing

         // any code here would not be reached
      }
      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" );
   }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?