程序运行—>异常—>程序中断
当有返回值时:
public int test(){ int n=10; try{ n++; //int m=9/0; //n++; return n; }catch(Exception e){ n=n+1; return n; }finally{ n=30; System.out.println("finally"); } }主要用于记录系统运行中一些重要的操作信息;
public class Mylog { public static void log(String message){ Date date = new Date();//获取当前时间 SimpleDateFormat simpleDateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//定义时间格式 StringBuilder sBuilder = new StringBuilder();//可以更好的连接字符串 sBuilder.append("# "); sBuilder.append(simpleDateFormat.format(date)); sBuilder.append(message); sBuilder.append(" #"); FileWriter fw =null; BufferedWriter bWriter=null; try { fw = new FileWriter("f:/Io/log.log",true);//要写入的文件 bWriter = new BufferedWriter(fw); bWriter.write(sBuilder.toString());//转换为字符串存入文件 bWriter.newLine();//每写一行就换行 fw.flush(); bWriter.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if (fw != null) { try { fw.close(); } catch (IOException e) { e.printStackTrace(); } } if (bWriter != null) { try { bWriter.close();//关闭流 } catch (IOException e) { e.printStackTrace(); } } } } }