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