java - Correct use of log4j and Exceptions -


i have doubt use of log4j exceptions. log messages logfile , don't know how deal exceptions. have use exception (because print in log file) or this:

try {         configurations.getinstance().getproperty("dynamodbprofilecredentials");         credentials = new profilecredentialsprovider( configurations.getinstance().getproperty("dynamodbprofilecredentials")).getcredentials();      } catch (exception e) {         log.error(new amazonclientexception("cannot load credentials credential profiles file. " +                 "please make sure credentials file @ correct " +                 "location (c:\\users\\your username\\credentials), , in valid format.",e)); 

or this

try {         configurations.getinstance().getproperty("dynamodbprofilecredentials");         credentials = new profilecredentialsprovider( configurations.getinstance().getproperty("dynamodbprofilecredentials")).getcredentials();      } catch (exception e) {         log.error("cannot load credentials credential profiles file. " +                 "please make sure credentials file @ correct " +                 "location (c:\\users\\your username\\credentials), , in valid format.",e);         throw new amazonclientexception(                 "cannot load credentials credential profiles file. " +                         "please make sure credentials file @ correct " +                         "location (c:\\users\\your username\\credentials), , in valid format.",                         e); 

thanks in advance.

exceptions signals/events; occur within bounds of application. logging of different topic.

i understand need logging helpful message within application. in case, trigger log event either on consumer of method or directly did it.

the easiest case be:

try {   configurations.getinstance().getproperty("dynamodbprofilecredentials");   credentials = new profilecredentialsprovider( configurations.getinstance().getproperty("dynamodbprofilecredentials")).getcredentials();  } catch (exception e) {   amazonclientexception ace = amazonclientexception(       "cannot load credentials credential profiles file. " +       "please make sure credentials file @ correct " +       "location (c:\\users\\your username\\credentials), , in valid format.", e);    log.error(ace.getmessage(), ace);   throw ace; } 

or

try {   configurations.getinstance().getproperty("dynamodbprofilecredentials");   credentials = new profilecredentialsprovider( configurations.getinstance().getproperty("dynamodbprofilecredentials")).getcredentials();  } catch (exception e) {   log.error("cannot load credentials credential profiles file. " +       "please make sure credentials file @ correct " +       "location (c:\\users\\your username\\credentials), , in valid format.",e);   throw new amazonclientexception(e); } 

the example above implementation not answer original question. answer original question, have have clear picture of api , responsibilities grow out of api design. want log within api or want give caller signal, exception occurred? or both (like in example above?).

before using exceptions or other way, propagate unexpected states, have answer question: how should case handled? should happen after exception? logging no exception; it's logging. after finding answer, can out appropriate way communicate unexpected states.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -