未捕获的异常未使用log4j记录到日志文件中

时间:2014-05-17 11:17:25

标签: java log4j dao

我遇到异常处理问题。我创建了自己的异常(DaoException):

然后我有我的读写函数,负责从文件加载和写入文件。这些功能看起来像:

public SudokuBoard read() throws DaoException {
    Object obj;
    try {
        fis = new FileInputStream(name);
        obj = ois.readObject();
        return (SudokuBoard) obj;

    } catch (FileNotFoundException ex) {
        throw new DaoException(DaoException.FILE_NOT_FOUND, ex);
    } catch (IOException ex) {
        throw new DaoException(DaoException.IO_EXCEPTION, ex);
    } catch (ClassNotFoundException ex) {
        throw new DaoException(DaoException.CLASS_NOT_FOUND, ex);
    } finally {
        try {
            fis.close();
        } catch (IOException ex) {

        }
    }

}


public void write(SudokuBoard obj) throws DaoException {
    try {
        FileOutputStream fos = new FileOutputStream(name);
    } catch (FileNotFoundException ex) {
        throw new DaoException(DaoException.FILE_NOT_FOUND, ex);
    }

}

然后我想在框架

中的函数中捕获这些异常
private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
    checkChangeFields();
    try {
        myDao.write(board);
    } catch (DaoException ex) {
        logger.error(ex.getLocalizedMessage(), ex);
    }
}                                          

private void loadButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
    try {
        board = (SudokuBoard) myDao.read();
    } catch (DaoException ex) {
        logger.error(ex.getLocalizedMessage(), ex);
    } 
    changeFields();
} 

记录器没有问题。它运作得很好,但我做错了什么?因为当我尝试加载不存在的文件时,我的程序崩溃,并且没有记录到文件的消息。并且在我自己的异常消息的控制台中打印出错误堆栈

DaoException:

public class DaoException extends ApplicationException {

private static final ResourceBundle messages;
//Message keys


static {
    Locale locale = Locale.getDefault(Locale.Category.DISPLAY);
    messages = ResourceBundle.getBundle("MyBundle", locale);
}

public static final String NULL_NAME = messages.getString("NULL_NAME");
public static final String OPEN_ERROR = messages.getString("OPEN_ERROR");
public static final String FILE_NOT_FOUND = messages.getString("FILE_NOT_FOUND");
public static final String IO_EXCEPTION = messages.getString("IO_EXCEPTION");
public static final String CLASS_NOT_FOUND = messages.getString("CLASS_NOT_FOUND");
public static final String GREAT = messages.getString("GREAT");

public DaoException(String message) {
    super(message);
}

public DaoException(String message, Throwable cause) {
    super(message, cause);
}

@Override
public String getLocalizedMessage() {
    String message;
    try {
        //Exception message is a key
        message = messages.getString(getMessage());
    } catch (MissingResourceException mre) {
        message = "No resource for " + getMessage() + "key";
    }
    return message;
}

}

堆叠错误:

May 17, 2014 1:22:46 PM pl.it.daos.FileSudokuBoardDao read
SEVERE: null
java.io.FileNotFoundException: aaaaa.dat (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:146)
    at java.io.FileInputStream.<init>(FileInputStream.java:101)
    at pl.it.daos.FileSudokuBoardDao.read(FileSudokuBoardDao.java:42)
    at pl.it.daos.FileSudokuBoardDao.read(FileSudokuBoardDao.java:22)
    at pl.it.gui.SudokuFrame.loadButtonActionPerformed(SudokuFrame.java:742)
    at pl.it.gui.SudokuFrame.access$500(SudokuFrame.java:24)
    at pl.it.gui.SudokuFrame$6.actionPerformed(SudokuFrame.java:655)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:708)
    at java.awt.EventQueue$4.run(EventQueue.java:706)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at pl.it.gui.SudokuFrame.changeFields(SudokuFrame.java:62)
    at pl.it.gui.SudokuFrame.loadButtonActionPerformed(SudokuFrame.java:746)
    at pl.it.gui.SudokuFrame.access$500(SudokuFrame.java:24)
    at pl.it.gui.SudokuFrame$6.actionPerformed(SudokuFrame.java:655)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:708)
    at java.awt.EventQueue$4.run(EventQueue.java:706)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

请帮忙

2 个答案:

答案 0 :(得分:0)

您的代码中存在轻微问题。在finally块中需要像这样做。如果fis!= null,则检查null。这将抛出一个未处理的空指针。这不会登录日志文件。

finally {
   try {
       if(fis != null) {
        fis.close();
       }
   } catch (IOException ex) {
        // Do some thing
   }

当一个线程由于未捕获的异常而终止时,它会调用printStackTrace(),这就是你一直看到异常控制台的原因。这些异常不会出现在您的lo4j.log文件中。但您可以在应用程序服务器日志文件中看到它们,例如如果你使用的是tomcat,你会在catalina.out中看到它们。你需要的是UncaughtExceptionHandler,它将记录你的日志文件。

See this how to add UncaughtExceptionHandler

答案 1 :(得分:0)

在finally块中简单检查fis!= null,您的代码将正常工作。

由于异常,DaoException永远不会到达错误处理程序。