Hi,

Logging and throwing is considered an anti pattern in some places since
same exception will be logged in multiple locations.  They recommend to
wrap the exception and throw.

catch(LowLevelException e){
     throw new HighlevelException("Highlevel operation failed etc...." , e)
}

Then exception hierarchy is not missed.

[1]
http://stackoverflow.com/questions/6639963/why-is-log-and-throw-condsidered-an-anti-pattern
[2] http://rolf-engelhard.de/2013/04/logging-anti-patterns-part-ii/

On Mon, Oct 13, 2014 at 7:16 AM, Roshan Deniyage <[email protected]> wrote:

> Hi,
> I think we need to catch exceptions, only if we can handle it or if we
> need to add more contextual information to the exception. In second case,
> creating new exception with more contextual information and throw it is
> okay. But, in that scenario, should not log and let the message is being
> logged later in the flow.
> When we handle it, we can log the message with more contextual information
> about the actual error Or in case of exceptions which can't be handled and
> populated to the upper layers, logging can be done in the most upper layer
> of the flow.
>
>
> Thanks,
> Roshan Deniyage
> Associate Technical Lead
> WSO2, Inc: http://wso2.com
>
> Mobile    :  +94 777636406
> Twitter    :  *https://twitter.com/roshku <https://twitter.com/roshku>*
> LinkedIn :  https://www.linkedin.com/in/roshandeniyage
>
> On Sun, Oct 12, 2014 at 11:01 PM, Firzhan Naqash <[email protected]> wrote:
>
>> Hi,
>>
>> As a standard practice we either throw the exception or log an error and
>> continue the operation.
>>
>> If that exception will cause serious inconsistency in the entire
>> functionality as a best practice we threw the exception.
>> Other wise we can log error and continue the operation.
>>
>> As Dinesh mentioned when try to do both, your stack trace will get
>> filled, thus it will be hard to debug it. You can find some tips over here
>> [1]
>>
>> [1]
>> http://howtodoinjava.com/2013/04/04/java-exception-handling-best-practices/
>>
>> Regards,
>> Firzhan
>>
>> On Sat, Oct 11, 2014 at 8:19 PM, Ravindra Ranwala <[email protected]>
>> wrote:
>>
>>> Hi Dinesh,
>>>
>>> The way you handle an Exception is subjective and depends on the
>>> context. But there is one important thing to note. That is never ever do
>>> something like this unless you have an extremely valid reason.
>>>
>>> try{
>>>      // Some code goes here
>>>      // ....
>>> } catch (Exception ignored) {
>>>     // Do Nothing .... Just IGNORE !!!!
>>> }
>>>
>>> This is called as swallowing an Exception and will make your day today
>>> life very difficult since it hides the exception. It neither logs the
>>> exception nor throws it. So It is very difficult to find errors with these
>>> sort of codes in your code base.
>>>
>>>
>>> Thanks &Regards,
>>>
>>>
>>>
>>> On Fri, Oct 10, 2014 at 10:16 AM, Waruna Perera <[email protected]>
>>> wrote:
>>>
>>>> Hi Danesh,
>>>>
>>>> In integration tests we used to log the exception and throw it. The
>>>> reason is the result will be included in the testng reports.
>>>>
>>>> Thanks
>>>>
>>>> On Wed, Oct 8, 2014 at 9:24 AM, Danesh Kuruppu <[email protected]> wrote:
>>>>
>>>>> One more question, why we do both log exception and throw it inside
>>>>> handleException?. It will result in multiple log messages in log file, for
>>>>> a single problem in the code.
>>>>>
>>>>> On Thu, Oct 2, 2014 at 9:32 AM, Dinesh J Weerakkody <[email protected]>
>>>>> wrote:
>>>>>
>>>>>> Hi,
>>>>>>
>>>>>> I'm just curious about the stranded way of handling exceptions in
>>>>>> WSO2 products. When I go through source code, I found that we use
>>>>>> handleException method in some places (in some places use deferent method
>>>>>> such as create new exceptions, catch, log and ignore, etc.) Can someone
>>>>>> explain the standard exception handling mechanism in WSO2 products and it
>>>>>> will be very helpful for newcomers also. (use cases for each case like 
>>>>>> when
>>>>>> to use what method).
>>>>>>
>>>>>>
>>>>>> When it comes to handleException method, I have another question.
>>>>>>
>>>>>>
>>>>>> *Current handle exception method*
>>>>>> public void handleException(String msg, Exception e) throws
>>>>>> ProductException{
>>>>>>         log.error(msg, e);
>>>>>>         throw new ProductException(msg, e);
>>>>>> }
>>>>>>
>>>>>> *Problem*
>>>>>> class One{
>>>>>>     public void methodOne() {
>>>>>>         try {
>>>>>>             Two two = new Two();
>>>>>>             two.methodTwo();
>>>>>>         } catch (WhateverException e) {
>>>>>>             handleException("Whatever exception message", e);
>>>>>>         }
>>>>>>     }
>>>>>> }
>>>>>>
>>>>>> class Two{
>>>>>>     public void methodTwo() {
>>>>>>         try {
>>>>>>             // Database manipulation code goes here
>>>>>>         } catch (SQLException e) {
>>>>>>             handleException("Cannot complete the transaction", e);
>>>>>>         }
>>>>>>     }
>>>>>> }
>>>>>>
>>>>>> In handleException method we always create a new Exception. When we
>>>>>> have multiple method calls, there will be duplicate stacktraces and the
>>>>>> actual exception is printed in very latter part of the stack trace.
>>>>>> Sometimes it is bit difficult to trace an error. Is there any specific
>>>>>> reason to create new exceptions inside handleException method, instead of
>>>>>> throwing same exception object if the exceptions is an instance of
>>>>>> ProductException like below sample.
>>>>>>
>>>>>> public void handleException(String msg, Exception e) throws
>>>>>> ProductException{
>>>>>>         log.error(msg, e);
>>>>>>         if(e instanceof ProductException)
>>>>>>                throw e;
>>>>>>         throw new ProductException(msg, e);
>>>>>> }
>>>>>>
>>>>>> Thank you.
>>>>>>
>>>>>>
>>>>>> --
>>>>>>
>>>>>> *Dinesh J. Weerakkody*
>>>>>> Software Engineer
>>>>>> WSO2 Inc.
>>>>>> lean | enterprise | middleware
>>>>>> M : +94 727 361788 | E : [email protected] | W : www.wso2.com
>>>>>>
>>>>>> _______________________________________________
>>>>>> Dev mailing list
>>>>>> [email protected]
>>>>>> http://wso2.org/cgi-bin/mailman/listinfo/dev
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>> --
>>>>>
>>>>> Danesh Kuruppu
>>>>> Software Engineer
>>>>> WSO2 Inc,
>>>>> Mobile: +94 (77) 1690552
>>>>>
>>>>> _______________________________________________
>>>>> Dev mailing list
>>>>> [email protected]
>>>>> http://wso2.org/cgi-bin/mailman/listinfo/dev
>>>>>
>>>>>
>>>>
>>>>
>>>> --
>>>> Waruna Perera
>>>> Senior Software Engineer - Test Automation
>>>> Mobile: +94 77 3867037
>>>> WSO2, Inc.; http://wso2.com/
>>>> lean . enterprise . middlewear.
>>>>
>>>> _______________________________________________
>>>> Dev mailing list
>>>> [email protected]
>>>> http://wso2.org/cgi-bin/mailman/listinfo/dev
>>>>
>>>>
>>>
>>>
>>> --
>>> Ravindra Ranwala
>>> Software Engineer
>>> WSO2, Inc: http://wso2.com
>>> <http://www.google.com/url?q=http%3A%2F%2Fwso2.com&sa=D&sntz=1&usg=AFQjCNEZvyc0uMD1HhBaEGCBxs6e9fBObg>
>>> Mobile: +94714198770
>>>
>>>
>>> _______________________________________________
>>> Dev mailing list
>>> [email protected]
>>> http://wso2.org/cgi-bin/mailman/listinfo/dev
>>>
>>>
>>
>> _______________________________________________
>> Dev mailing list
>> [email protected]
>> http://wso2.org/cgi-bin/mailman/listinfo/dev
>>
>>
>
> _______________________________________________
> Dev mailing list
> [email protected]
> http://wso2.org/cgi-bin/mailman/listinfo/dev
>
>


-- 

Udara Liyanage
Software Engineer
WSO2, Inc.: http://wso2.com
lean. enterprise. middleware

web: http://udaraliyanage.wordpress.com
phone: +94 71 443 6897
_______________________________________________
Dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/dev

Reply via email to