注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Study Desk

Share my desk with you!......

 
 
 

日志

 
 

异常处理复习(JAVA)  

2007-01-07 13:17:57|  分类: 爪哇岛 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

不要偷看结果,读完这个题目然后把结果写出来,如果你的结果没错,那恭喜你掌握了这块内容。记得不要偷看哦...

/**
 * review the Exception Handling
 * Demonstration of the try-cathc-finally exception handling mechanism.
 */
package com.roobo.study;

/**
 * @author Coolsun
 * @Date 01.07.2007
 */
public class ReviewingException {

 /**
  * @param execute application
  */
 public static void main(String[] args) {
  // call method throwException
  try{
   throwException();
  }
  // catch Exception thrown by method throwException
  catch(Exception e){
   System.err.println("Exception handled in main");
  }

  doesNotThrowException();
 }
 
 // demonstrate try/catch/finally
 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 thrown in try block
  catch(Exception e){
   System.err.println("Exception handled in method throwException");
   throw e;    // rethrow for further processing
   
   // any code here would not be reached
  }
  
  // this block executes regardless of what occurs in try/catch
  finally{
   System.err.println("Finally executed in throwException");
  }
  
        // any code here would not be reached
 }
 
 // demonstrate finally when no exception occurs
 public static void doesNotThrowException(){
  // try block does not throw an exception
  try{
   System.out.println("Method doesNotThrowException");
  }
  // catch does not execute, because no exception thrown
  catch(Exception e){
   System.err.println(e.toString());
  }
  // this block executes regardless of what occurs in try/catch
  finally{
   System.err.println("Finally executed in doesNotThrowException");
  }
  
  System.out.println("End of method doesNotThrowException");
 }

}

 

 

 

 

 

 

程序运行结果:

Method throwException
Exception handled in method throwException
Finally executed in throwException
Exception handled in main
Method doesNotThrowException
Finally executed in doesNotThrowException
End of method doesNotThrowException

  评论这张
 
阅读(255)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017