javaexception.txt

来自「java异常处理」· 文本 代码 · 共 50 行

TXT
50
字号
OutputStreamWriter out = ... 
java.sql.Connection conn = ... 
try { 
  Statement stat = conn.createStatement(); 
  ResultSet rs = stat.executeQuery( 
    "select uid, name from user"); 
  while (rs.next()) 
  { 
    out.println("ID:" + rs.getString("uid") + 
      ",姓名: " + rs.getString("name")); 
  } 
} 
catch(SQLException sqlex) 
{ 
  out.println("警告:数据不完整"); 
  throw new ApplicationException( 
    "读取数据时出现SQL错误", sqlex); 
} 
catch(IOException ioex) 
{ 
  throw new ApplicationException( 
    "写入数据时出现IO错误", ioex); 
} 
finally 
{ 
  if (conn != null) { 
    try { 
      conn.close(); 
    } 
    catch(SQLException sqlex2) 
    { 
      System.err(this.getClass().getName() + 
        ".mymethod - 不能关闭数据库连接: " + 
      sqlex2.toString()); 
    } 
  } 

  if (out != null) { 
    try { 
      out.close(); 
    } 
    catch(IOException ioex2) 
    { 
      System.err(this.getClass().getName() + 
        ".mymethod - 不能关闭输出文件" + 
      ioex2.toString()); 
    } 
  } 
} 

⌨️ 快捷键说明

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