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 <ravin...@wso2.com> 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 <waru...@wso2.com> 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 <dan...@wso2.com> 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 <dine...@wso2.com>
>>> 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 : dine...@wso2.com | W : www.wso2.com
>>>>
>>>> _______________________________________________
>>>> Dev mailing list
>>>> Dev@wso2.org
>>>> http://wso2.org/cgi-bin/mailman/listinfo/dev
>>>>
>>>>
>>>
>>>
>>> --
>>>
>>> Danesh Kuruppu
>>> Software Engineer
>>> WSO2 Inc,
>>> Mobile: +94 (77) 1690552
>>>
>>> _______________________________________________
>>> Dev mailing list
>>> Dev@wso2.org
>>> 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
>> Dev@wso2.org
>> 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
> Dev@wso2.org
> http://wso2.org/cgi-bin/mailman/listinfo/dev
>
>
_______________________________________________
Dev mailing list
Dev@wso2.org
http://wso2.org/cgi-bin/mailman/listinfo/dev

Reply via email to