From 2747e0cd126be93b127a4bb9508178d74d9df39c Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Thu, 13 Aug 2015 15:19:56 +0200 Subject: [PATCH] =?utf8?q?More=20rewrites=20for=20thrown=20exceptions=20Si?= =?utf8?q?gned-off-by:Roland=20H=C3=A4der=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../application/AddressbookApplication.java | 7 ++ .../client/console/ConsoleClient.java | 83 ++++++++++--------- .../client/gui/AddressbookFrame.java | 12 ++- .../AddressbookContactDatabaseFrontend.java | 2 +- .../contact/AddressbookContactManager.java | 15 ++-- .../contact/ManageableAddressbookContact.java | 14 +++- 6 files changed, 80 insertions(+), 53 deletions(-) diff --git a/Addressbook/src/org/mxchange/addressbook/application/AddressbookApplication.java b/Addressbook/src/org/mxchange/addressbook/application/AddressbookApplication.java index e4ac7a9..4be3359 100644 --- a/Addressbook/src/org/mxchange/addressbook/application/AddressbookApplication.java +++ b/Addressbook/src/org/mxchange/addressbook/application/AddressbookApplication.java @@ -338,6 +338,13 @@ public class AddressbookApplication extends BaseAddressbookSystem implements App */ private void start (final String args[]) { this.getLogger().info("Program is started."); //NOI18N + try { + // Init properties file + this.initProperties(); + } catch (final IOException ex) { + // Something bad happened + this.abortProgramWithException(ex); + } // Parse arguments this.parseArguments(args); diff --git a/Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java b/Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java index a947620..6ed15fb 100644 --- a/Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java +++ b/Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java @@ -35,6 +35,7 @@ import org.mxchange.addressbook.menu.item.console.ConsoleMenuItem; import org.mxchange.jcore.application.Application; import org.mxchange.jcore.contact.Contact; import org.mxchange.jcore.contact.Gender; +import org.mxchange.jcore.exceptions.BadTokenException; import org.mxchange.jcore.exceptions.UnhandledUserChoiceException; import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException; @@ -342,41 +343,43 @@ public class ConsoleClient extends BaseAddressbookClient implements AddressbookC // Get manager and cast it ManageableAddressbookContact manager = (ManageableAddressbookContact) this.getManager(); - // @TODO Rewrite this ugly switch() block - switch (choice) { - case '1': - try { - // Enter/add own data - manager.doEnterOwnData(); - } catch (final ContactAlreadyAddedException ex) { - // Already added - this.outputMessage("Sie haben bereits Ihre eigenen Daten eingegeben."); - } - break; - - case '2': // Change own data - manager.doChangeOwnData(); - break; - - case '3': // Add new addess - manager.doAddOtherAddress(); - break; - - case '4': // List contacts - manager.doListContacts(); - break; - - case '5': // Search addresses - manager.doSearchContacts(); - break; - - case '6': // Change other addess - manager.doChangeOtherAddress(); - break; - - case '7': // Delete other address - manager.doDeleteOtherAddress(); - break; + // Try it! + try { + // @TODO Rewrite this ugly switch() block + switch (choice) { + case '1': + try { + // Enter/add own data + manager.doEnterOwnData(); + } catch (final ContactAlreadyAddedException ex) { + // Already added + this.outputMessage("Sie haben bereits Ihre eigenen Daten eingegeben."); + } + break; + + case '2': // Change own data + manager.doChangeOwnData(); + break; + + case '3': // Add new addess + manager.doAddOtherAddress(); + break; + + case '4': // List contacts + manager.doListContacts(); + break; + + case '5': // Search addresses + manager.doSearchContacts(); + break; + + case '6': // Change other addess + manager.doChangeOtherAddress(); + break; + + case '7': // Delete other address + manager.doDeleteOtherAddress(); + break; case '0': { try { @@ -388,9 +391,13 @@ public class ConsoleClient extends BaseAddressbookClient implements AddressbookC } break; - default: - // @TODO throw own exception - throw new UnhandledUserChoiceException(MessageFormat.format("Choice '{0}' not handled yet.", choice)); //NOI18N + default: + // @TODO throw own exception + throw new UnhandledUserChoiceException(MessageFormat.format("Choice '{0}' not handled yet.", choice)); //NOI18N + } + } catch (final IOException | BadTokenException ex) { + // Something bad happened + this.abortProgramWithException(ex); } // Trace message diff --git a/Addressbook/src/org/mxchange/addressbook/client/gui/AddressbookFrame.java b/Addressbook/src/org/mxchange/addressbook/client/gui/AddressbookFrame.java index 8661516..4bdb6e9 100644 --- a/Addressbook/src/org/mxchange/addressbook/client/gui/AddressbookFrame.java +++ b/Addressbook/src/org/mxchange/addressbook/client/gui/AddressbookFrame.java @@ -54,6 +54,7 @@ import org.mxchange.jcore.client.Client; import org.mxchange.jcore.client.gui.ClientFrame; import org.mxchange.jcore.contact.Contact; import org.mxchange.jcore.contact.Gender; +import org.mxchange.jcore.exceptions.BadTokenException; import org.mxchange.jcore.exceptions.FrameAlreadyInitializedException; import org.mxchange.jcore.model.swing.contact.ContactTableModel; @@ -209,7 +210,7 @@ public class AddressbookFrame extends BaseAddressbookSystem implements ClientFra * @param client Client instance */ @Override - public void setupFrame (final Client client) { + public void setupFrame (final Client client) throws IOException, BadTokenException { // Debug line this.getLogger().trace(MessageFormat.format("client={0}: CALLED!", client)); //NOI18N @@ -682,6 +683,9 @@ public class AddressbookFrame extends BaseAddressbookSystem implements ClientFra // Already added, log away // @TODO maybe output message here? self.logException(ex); + } catch (final IOException | BadTokenException ex) { + // Somethind bad happened here + // @TODO Output error message here? } } }); @@ -702,7 +706,11 @@ public class AddressbookFrame extends BaseAddressbookSystem implements ClientFra @Override public void actionPerformed (final ActionEvent e) { ManageableAddressbookContact manager = (ManageableAddressbookContact) self.getClient().getManager(); - manager.doChangeOwnData(); + try { + manager.doChangeOwnData(); + } catch (final IOException | BadTokenException ex) { + self.logException(ex); + } } }); diff --git a/Addressbook/src/org/mxchange/addressbook/database/frontend/contact/AddressbookContactDatabaseFrontend.java b/Addressbook/src/org/mxchange/addressbook/database/frontend/contact/AddressbookContactDatabaseFrontend.java index 2b93bf8..912ad5b 100644 --- a/Addressbook/src/org/mxchange/addressbook/database/frontend/contact/AddressbookContactDatabaseFrontend.java +++ b/Addressbook/src/org/mxchange/addressbook/database/frontend/contact/AddressbookContactDatabaseFrontend.java @@ -48,7 +48,7 @@ public class AddressbookContactDatabaseFrontend extends BaseDatabaseFrontend imp * * @param manager Manager instance * @throws org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException If the database backend is not supported - * @throws java.sql.SQLException If any SQL error occurs + * @throws java.sql.SQLException If an SQL error occurs */ public AddressbookContactDatabaseFrontend (final AddressbookContactManager manager) throws UnsupportedDatabaseBackendException, SQLException { // Call own constructor diff --git a/Addressbook/src/org/mxchange/addressbook/manager/contact/AddressbookContactManager.java b/Addressbook/src/org/mxchange/addressbook/manager/contact/AddressbookContactManager.java index 980c64b..ab71513 100644 --- a/Addressbook/src/org/mxchange/addressbook/manager/contact/AddressbookContactManager.java +++ b/Addressbook/src/org/mxchange/addressbook/manager/contact/AddressbookContactManager.java @@ -64,7 +64,7 @@ public class AddressbookContactManager extends BaseManager implements Manageable * a client instance. * * @param client Client instance to use - * @throws org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException If the database backend is not supported + * @throws org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException If the configured database backend is not supported * @throws java.sql.SQLException If an SQL error occurs */ public AddressbookContactManager (final Client client) throws UnsupportedDatabaseBackendException, SQLException { @@ -243,7 +243,7 @@ public class AddressbookContactManager extends BaseManager implements Manageable * Allows the user to change his/her own data */ @Override - public void doChangeOwnData () { + public void doChangeOwnData () throws IOException , BadTokenException{ // Trace message this.getLogger().trace("CALLED!"); //NOI18N @@ -294,7 +294,7 @@ public class AddressbookContactManager extends BaseManager implements Manageable * Asks user for own data */ @Override - public void doEnterOwnData () throws ContactAlreadyAddedException { + public void doEnterOwnData () throws ContactAlreadyAddedException, IOException , BadTokenException { // Trace message this.getLogger().trace("CALLED!"); //NOI18N @@ -599,7 +599,7 @@ public class AddressbookContactManager extends BaseManager implements Manageable * @return Value from given row/column */ @Override - public Object getValueFromRowColumn (final int rowIndex, final int columnIndex) { + public Object getValueFromRowColumn (final int rowIndex, final int columnIndex) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { // Trace message this.getLogger().trace(MessageFormat.format("rowIndex={0},columnIndex={1} CALLED!", rowIndex, columnIndex)); @@ -644,7 +644,7 @@ public class AddressbookContactManager extends BaseManager implements Manageable * @return Whether own contact is already added */ @Override - public boolean isOwnContactAdded () { + public boolean isOwnContactAdded () throws IOException, BadTokenException { // Trace message this.getLogger().trace("CALLED!"); //NOI18N @@ -697,10 +697,7 @@ public class AddressbookContactManager extends BaseManager implements Manageable // Add contact to internal list this.addContact(contact); - } catch (final ContactAlreadyAddedException ex) { - // Abort here - this.abortProgramWithException(ex); - } catch (final BadTokenException ex) { + } catch (final ContactAlreadyAddedException | BadTokenException | IOException ex) { // Abort here this.abortProgramWithException(ex); } diff --git a/Addressbook/src/org/mxchange/addressbook/manager/contact/ManageableAddressbookContact.java b/Addressbook/src/org/mxchange/addressbook/manager/contact/ManageableAddressbookContact.java index 6511730..77d39d3 100644 --- a/Addressbook/src/org/mxchange/addressbook/manager/contact/ManageableAddressbookContact.java +++ b/Addressbook/src/org/mxchange/addressbook/manager/contact/ManageableAddressbookContact.java @@ -16,9 +16,11 @@ */ package org.mxchange.addressbook.manager.contact; +import java.io.IOException; import org.mxchange.addressbook.exceptions.ContactAlreadyAddedException; import org.mxchange.jcore.contact.Contact; import org.mxchange.jcore.contact.Gender; +import org.mxchange.jcore.exceptions.BadTokenException; import org.mxchange.jcore.manager.database.ManageableDatabase; /** @@ -173,8 +175,10 @@ public interface ManageableAddressbookContact extends ManageableDatabase { /** * Let the user change own data + * @throws java.io.IOException If an IO error was found + * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found */ - public void doChangeOwnData (); + public void doChangeOwnData () throws IOException , BadTokenException; /** * Let the user delete other address @@ -184,8 +188,10 @@ public interface ManageableAddressbookContact extends ManageableDatabase { /** * Asks user for own data * @throws org.mxchange.addressbook.exceptions.ContactAlreadyAddedException If own contact is already added + * @throws java.io.IOException If an IO error was found + * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found */ - public void doEnterOwnData () throws ContactAlreadyAddedException; + public void doEnterOwnData () throws ContactAlreadyAddedException, IOException , BadTokenException; /** * Searches address book for a contact @@ -197,6 +203,8 @@ public interface ManageableAddressbookContact extends ManageableDatabase { * isOwnContact flag * * @return Whether own contact is already added + * @throws java.io.IOException If an IO error occurs + * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found */ - public boolean isOwnContactAdded (); + public boolean isOwnContactAdded () throws IOException, BadTokenException; } -- 2.39.5