1

I have problem with Exception handling. I created own exception(DaoException):

then i have my read and write functions, which are responsible for loading from file, and writing to file. these functions looks, like that:

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);
    }

}

then i want to catch these exceptions in my functions in frame

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();
} 

There is no problem with logger. It works great, but what am i doing wrong ? Because when i try to load a file which doesnt exist there is a crash of my program, and no messege is logged to file. and Stack of errors is printed in console insted of my own exception messege

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;
}

}

Stack with errors:

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)

Pls help

Holger Just
  • 52,918
  • 14
  • 115
  • 123
karpiu08
  • 35
  • 1
  • 7

2 Answers2

0

There is slight problem in your code. In the finally block need to do it like this. Check for null if fis != null. This will throw a null pointer which is unhandled. This will not log in log file.

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

When a thread is terminated due to uncaught exception it calls printStackTrace() and that is why you see the exception console all the time. These exceptions will not show up in your lo4j.log files. But you can see them in your application server log files for e.g. if you are using tomcat you will see them in catalina.out. What you need is UncaughtExceptionHandler that will log to your log file.

See this how to add UncaughtExceptionHandler

Community
  • 1
  • 1
vkg
  • 1,839
  • 14
  • 15
  • i added if statements, but it still dont work. I have to add UncaughtExceptionHandler ? there is no other way ? – karpiu08 May 17 '14 at 12:27
  • Yes IMHO you should add UncaughtExceptionHandler that is the right way to log uncaught exceptions. – vkg May 17 '14 at 12:28
  • FileInputStream implements AutoCloseable; you could go with "try ( FileInputStream fis = new FileInputStream(name)) { ... }" and let try-with-resources close the stream for you. – Marcel Steinbach May 17 '14 at 12:29
  • @vkg i understand , but its project for univeristy, and tutor didnt mention anything about UncaughtExceptionHandler so i am not sure, if it is necessary, i will try repair this ;p – karpiu08 May 17 '14 at 12:35
  • @vkg ok, i added this handler, and now, i file is a messege similar to these in console. But the purpose of creating this DaoException wasnt that, i will get in console messege definied in DaoException, insted of this list ? – karpiu08 May 17 '14 at 12:54
  • @user3619290 Sorry not sure what you mean by the previous comment. – vkg May 17 '14 at 12:57
  • @vkg I created DaoException, and there are some Strings: for each Exception there is different messege. And i want, that everytime, i catch filenotfoundexception, messege responsible for this error be printed in console and in file ;) ( insted of this list of errors ) – karpiu08 May 17 '14 at 12:59
  • @user3619290 if you mean the stack trace by list of errors. Exceptions are always logged with full stack trace. But if you just want to see the error message instead of full stack just use logger.error(ex.getLocalizedMessage()); INSTEAD OF logger.error(ex.getLocalizedMessage(), ex); – vkg May 17 '14 at 13:31
0

Simple check for fis != null in finally block and your code will work fine.

Due to exception there DaoException never reach to error handlers.

Satwant
  • 11
  • 2