1

I want a hack here. What I am trying out is to execute some of the business logic after throwing an exception. The business logic is defined in the catch block. I don't want to keep the throw statement at the end of the catch block. Is there any hack I can try? What I am thinking is to create a thread in that point and inside that throw the exception. I am not sure whether it will work or not. Please help me out.

package demo1.web;

import demo.exceptions.SupportInfoException;

public class TestInnerException {
void testIt() throws Exception{
    try{
        int i=0;
        int j=1/i;
    }catch(Exception e){
        System.out.println("business logic .. .. . . ");
        throw e;
        // I want to excute these below line ,
         but it is unreachable because you aleready throwing the excetion ..
        //any Hacks for it ?
         System.out.println("Lines after throwing the exception ... .");

    }

    System.out.println("I want this logic to be run . . . . .");

}
public static void main(String[] args) throws SupportInfoException, Exception {
    TestInnerException t = new TestInnerException();
    t.testIt();


}
void testRunTimeEx(){
        int i=0;
        int j=1/i;

}
}

Let me tell you my scenario , I am having @ExceptionHandler annotation to handle all the exception as described in this post . https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc#user-content-sample-application Some complex scenario , cant explain why exactly . What i want here is if it is handled by catch block then i want that to be triggered in @ExceptionHandler because there i have some other Business logic . The problem is @ExceptionHandler is not triggered unless exception is thrown. I want that to be triggered if it entered the catch block. But Its not triggered if the catch block doesnt throw any exception.

Business logic in this context I mean as some Logging Function.

ajayramesh
  • 3,576
  • 8
  • 50
  • 75
  • 3
    I simply don't understand why anyone would want to do that?. If you want to execute those lines, then don't throw exception there – TheLostMind Aug 31 '15 at 08:49
  • You should not have any business login in exception handlers / catch / finally blocks. On resource cleanup – TheLostMind Aug 31 '15 at 08:58
  • 1
    Rethink your design. `An exception is thrown when a fundamental assumption of the current code block is found to be false`. Have a look at [this](http://stackoverflow.com/questions/77127/when-to-throw-an-exception). You should NOT be able to continue after you've thrown an exception. – Ghazanfar Aug 31 '15 at 09:07
  • @MohammadGhazanfar Exactly. Looks like a design smell. – Suresh Atta Aug 31 '15 at 09:13

1 Answers1

2

It's just not hack, perhaps won't make much sense.

1) If you want to throw some exception, throw right away.

2) If you want to do some business logic on exception raise, do it and then throw the exception.

But if you want to throw and do some, not quite possible without any dirty code here. Seems a code smell here to me.

Edit after the question Edit :

Though it is logging, still you can log before throwing it, there is no point to generate log after throwing it.

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307