⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rethrowexceptiondemo.java

📁 JAVA学习源代码,大家可以好好参考,请多提宝贵意见
💻 JAVA
字号:
 //RethrowExceptionDemo.java
 //class ThrowExceptionDemo
 public class RethrowExceptionDemo{
  public static void main( String args[] ){
    
    //调用方法throwException()
    try {
      throwException();
    }

   //将方法throwException()抛出的异常在catch块中处理
   catch ( Exception exception ){
     System.out.println( "Exception handled in main" );
   }
   doesNotThrowException();
 }

 public static void throwException() throws Exception{
   
   //try语句块产生一个异常
      try {
     System.out.println( "\n***Calling method throwException***\n" );
     throw new Exception();
         }

   //打印异常信息,并重新抛出异常
     catch ( Exception e ){
     System.out.println(
               "Exception handled in static method throwException" );
     System.out.println("Rethrow the exception object " 
                                                                      + "in static method throwException");
     throw e;
    }
 }

 //
 public static void doesNotThrowException(){
   
   //try语句块产生异常
   try {
     System.out.println( "\n***Calling method doesNotThrowException***\n");
     throw new Exception();
   }

   //catch语句块捕获异常,并打印异常信息,但不重新抛出异常
   catch( Exception exception ){
     System.out.println("Exception handled in static method doesNotThrowException" );
     System.out.println("Does not Rethrow the exception object " 
                                                                     + "in static method doesNotThrowException");
   }
  }
}

⌨️ 快捷键说明

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