java - Is Exception Enrichment a viable pattern? -


i came across article jakob jenkov enrichable exceptions. idea!

i've been java developer 4 years, , still annoyed trying figure out sqlexcepions, ioexceptions, , random exceptions thrown third-party libraries. end putting thought when appropriate tho rethrow exception, or wrap 1 custom exception, if write custom exception, put it? in same package class i'm writing, or in central "exceptions" package? i've toyed declaring custom exceptions inner classes since seem pretty one-off.

anyways, jenkov's idea outlined here: http://tutorials.jenkov.com/java-exception-handling/exception-enrichment.html

using single enrichable exception outlines seems attractive me seems pretty handily side-step of frustrations. don't think i've ever seen used in of libraries i've used. there reason that? there major flaw approach i'm missing?

simple summary:

public class appexception extends exception{     public appexception(string errcode, string context, string usermessage, throwable t){}      public appexception(string, errcode, string context, string usermessage) {}      public void addinfo(string errcode, string context, string usermessage){} }  public class myapp {     public static void main(string [] args) {         myapp app = new myapp();              app.start(args[0], args[1]);             }      public void start(string username, string password) {         try {             user user = logincontroller.login(username, password);         } catch (appexception e) {             if (e.getcode().contains("incorrect_password")) {                 logger.warn(e.tostring(), e);                 // prompt user new password , try again.             } else {                 logger.error(e.tostring(), e);             }         } catch (exception e) {             logger.error("unexpected exception.", e);         }     } }  public class logincontroller {     public user login(string username, string password) {         try {             if (userdao.checkpassword(username, password)) {                 // build , return user object.             }         } catch (appexcption e) {             e.addinfo("login_failed", "logincontroller", "failed login");             throw e;         }     } }  public class userdao {     public boolean checkpassword(string username, string password) {         try {             // check password against db.              if (passwordiscorrect) {                 return true;             } else {                 throw new appexception("incorrect_password", "userdao", "password user " + username + " not correct.");             }         } catch (sqlexception e) {             throw new appexception("db_connection_err", "userdao", "can't connect database", e);         }     } } 

actual implementation of exception class available in jenkov's article. in above code example, if db unreachable error log like:

[login_failed, loggincontroller], [db_connection_err, userdao] [login_failed, loggincontroller]: failed login [db_connection_err, userdao]: can't connect database <<sqlexception stacktrace here>> 

but if incorrect password entered be:

[login_failed, loggincontroller], [incorrect_password, userdao] [login_failed, loggincontroller]: failed login [db_connection_err, userdao]: password user [passed in username] not correct. 

i'd add enum or error codes make selective handling easier, , ensure unique. error message gives clear concise view of when wrong, , it's easy add additional information error climes call stack developer able recreate error.

so repeat question. why isn't used more widely? there major flaw in approach i'm missing?


Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -