。。。

来源:互联网 发布:中国网络日报社 编辑:程序博客网 时间:2024/05/27 21:04

public class ExceptionTest
{
 private static final Logger log = Logger.getLogger(ExceptionTest.class);
 public void login(String name, String password) throws MyException
 {
  if (!"fudan".equals(name) && !"wafd@1314".equals(password))
  {
   throw new MyException("user name or password error!","111");
  }
 }
 
 public void writeMessage(String message, String filePath) throws MyException, FileNotFoundException
 {
  Writer writer = null;
  log.debug("write message.....");
  try {
   writer = new FileWriter(new File(filePath));
   writer.write(message);
  }
  catch (IOException e)
  {
   log.error("file not found!" + e);
   throw new MyException(filePath + " is not exists", "22034", e);
//   throw new FileNotFoundException();
  }
  finally
  {
   if (null != writer)
   {
    try
    {
     writer.close();
    }
    catch (IOException e)
    {
     log.error("close resource error" + e);
    }
   }
  }
  
  log.debug("notify message");
  //同步
 }
 
 public static void main(String[] args) {
  ExceptionTest test = new ExceptionTest();
  
  try {
   test.login("fudan", "wafd@1314");
   test.writeMessage("werwer", "I:/werwer");
  }
  catch (MyException e)
  {
   System.out.println(e);
  }
  //jppewrtmgpf
 catch (FileNotFoundException e) {
   e.printStackTrace();
  }
 }
}

原创粉丝点击