专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Java Exception

关于捕获异常

发布时间:2010-06-05 12:33:18 文章来源:www.iduyao.cn 采编人员:星星草

// Paying attention to exceptions
  // in constructors.
  import java.io.*;
 
  class InputFile {
  private BufferedReader in;
  InputFile(String fname) throws Exception{
  try {
  in =
  new BufferedReader(
  new FileReader(fname));
  // Other code that might throw exceptions
  } catch(FileNotFoundException e) {
  System.err.println(
  "Could not open " + fname);
  // Wasn't open, so don't close it
  throw e;
  } catch(Exception e) {
  // All other exceptions must close it
  try {
  in.close();
  } catch(IOException e2) {
  System.err.println(
  "in.close() unsuccessful");
  }
  throw e; // Rethrow
  } finally {
  // Don't close it here!!!
  }
  }
  String getLine() {
  String s;
  try {
  s = in.readLine();
  } catch(IOException e) {
  System.err.println(
  "readLine() unsuccessful");
  s = "failed";
  }
  return s;
  }
  void cleanup() {
  try {
  in.close();
  } catch(IOException e2) {
  System.err.println(
  "in.close() unsuccessful");
  }
  }
  }
 
  public class Cleanup {
  public static void main(String[] args) {
  try {
  InputFile in =
  new InputFile("Cleanup.java");
  String s;
  int i = 1;
  while((s = in.getLine()) != null)     

友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: