From df3a5f95efbf58451ed316c02516ceb50025ef99 Mon Sep 17 00:00:00 2001 From: Jonas Fonseca Date: Thu, 30 Aug 2007 19:31:57 +0200 Subject: [PATCH] Cleanup exception handling This changes the server side code not to save an exception and rethrow it in the "finally" block. The "finally" block is guaranteed to always run regardless of wether the exception is temporarily held back. So instead of doing: Exception ex = null; try { /* main code */ } catch (Exception e) { ex = e; } finally { /* cleanup code */ if (ex != null) throw ex; } we now do: try { /* main code */ } finally { /* cleanup code */ } --- src/authentication/AbstractAuthenticator.java | 11 +---------- src/authentication/Authenticator.java | 14 ++------------ src/moteaccess/AbstractMoteAccess.java | 9 +++------ src/motedata/MoteData.java | 9 +++------ 4 files changed, 9 insertions(+), 34 deletions(-) diff --git a/src/authentication/AbstractAuthenticator.java b/src/authentication/AbstractAuthenticator.java index 03c14fd..1050680 100644 --- a/src/authentication/AbstractAuthenticator.java +++ b/src/authentication/AbstractAuthenticator.java @@ -26,27 +26,18 @@ public abstract class AbstractAuthenticator { if (!checkCredentials(session_id, credentials)) return false; - boolean success = false; SQLHelper sql = null; - Exception ex = null; try { String auth = "update session set auth=1 where id=" + session_id; sql = new SQLHelper(); sql.openDB(); - success = (sql.update(auth) == 1); - - } catch (Exception e) { - ex = e; + return sql.update(auth) == 1; } finally { sql.closeDB(); - if (ex != null) - throw ex; } - - return success; } /** This method should be implemented to check the credentials diff --git a/src/authentication/Authenticator.java b/src/authentication/Authenticator.java index db86bc8..3713823 100644 --- a/src/authentication/Authenticator.java +++ b/src/authentication/Authenticator.java @@ -18,6 +18,7 @@ public class Authenticator extends AbstractAuthenticator { protected boolean checkCredentials(String session_id, Credential[] credentials) throws Exception { + SQLHelper sql = null; String checkSQL = "select 1 from user u, project p, user_project up " + "where u.login='" + credentials[1].getValue() + "' " + "and u.password=md5('" + credentials[2].getValue() + "') " + @@ -25,25 +26,14 @@ public class Authenticator extends AbstractAuthenticator { "and up.user_id=u.id " + "and up.project_id=p.id"; - SQLHelper sql = null; - Exception ex = null; - boolean result = false; - try { sql = new SQLHelper(); sql.openDB(); - result = sql.retrieve(checkSQL).next(); - - } catch (Exception e) { - ex = e; + return sql.retrieve(checkSQL).next(); } finally { sql.closeDB(); - if (ex != null) - throw ex; } - - return result; } } diff --git a/src/moteaccess/AbstractMoteAccess.java b/src/moteaccess/AbstractMoteAccess.java index e9080d0..2b84905 100644 --- a/src/moteaccess/AbstractMoteAccess.java +++ b/src/moteaccess/AbstractMoteAccess.java @@ -25,7 +25,7 @@ public abstract class AbstractMoteAccess { { boolean access = false; SQLHelper sql = null; - Exception ex = null; + try { // Check for current privileged session String query = "select priv_session_id from mote " + @@ -50,14 +50,11 @@ public abstract class AbstractMoteAccess { " where id="+mote_id; sql.execute(query); } - } catch (Exception e) - { - ex = e; + return access; + } finally { sql.closeDB(); - if (ex != null) throw ex; } - return access; } /** diff --git a/src/motedata/MoteData.java b/src/motedata/MoteData.java index c00a7c6..f908378 100644 --- a/src/motedata/MoteData.java +++ b/src/motedata/MoteData.java @@ -22,7 +22,6 @@ public class MoteData { { String query,select; SQLHelper sql = null; - Exception ex = null; ResultSet rs = null; SimpleTable result = null; @@ -67,14 +66,12 @@ public class MoteData { } i++; } - } catch (Exception e) - { - ex = e; + + return result; + } finally { sql.closeDB(); - if (ex != null) throw ex; } - return result; } private String getMoteAttrSubquery(String colname,String type_id) -- 2.11.4.GIT