简体   繁体   中英

How to find out , if same method is getting called from catch block or not in java?

I have one method for eg void abc(){} and this method is getting called from my main() method directly as well as from it's exception block ie from catch block too.

Now I wants to find out inside abc() method , whether abc() is getting called normally (ie from main() ) or it is called in exception case (ie from catch() block).

I tried this , using Thread.getCurrentStackTrace() , however it always returns main() method (as method name), as in both cases method will always remain same.

Important point here is , I can not change signature of abc()method and I can not set any flag inside main() method or in catch() block , with this limitations, I am trying to find out , how to check from where abc() get called or in simple words....how to find out whether exception is thrown or not inside abc() method.

Please suggest!

Using StackTraceElement , you might get the line number from which you can figure out whether abc() is called from catch block of main() or not. See here . It is something similar to what you have already tried but there you get the line number .

Note: This will work only if you have source code of main method to see line number. Won't work if main is in external jar whose source code is not available to you.

EDIT: Also, this won't work if the external jar is created without line number debug information.

Thread.getCurrentStackTrace() will obviously don't work since whether you call it from main or it is called from catch in either case thread will remain same.

Than what are the options, first you can do what @Eran suggested and I am not repeating that answer, If you don't want to change the method signature another thing that you can do is to set/unset a boolean variable before calling abc() from catch block.. see below example

private static boolean isCalledFromCatch = false;
public static void method ()
{
    if(isCalledFromCatch){
        //Catch specific processing here
    }else{
        //main specific processing here
    }
}

public static void main (String[] args)
{
    try {
      isCalledFromCatch = false;
        abc();
    }
    catch (Exception exc) {
        isCalledFromCatch = true;
        abc();
    }
}

However if one function needs to be called from try and catch both, better will be to call it from finally block. That way you don't need to call it from two separate places.

You can pass a boolean parameter to your method that would let it know where it was called from.

public static void abc(boolean isError)
{
    if (isError) {
        // method was called from catch block
    }
}

public static void main (String[] args)
{
    try {
        ...
        abc(false);
        ...
    }
    catch (Exception exc) {
        abc(true);
    }
}

I could think of some solutions:

  1. with an additional input for abc() , detect where your method called. If you want to change method abc's functionallity if an error occured use this approach.

  2. use log. Print log where you see the error. and I recommend this approach, because your method signature remain the same.

  3. add an static variable in your class(like a boolean) and set this variable when error occurred and check this variable in abc .

You can add a String parameter to your method:

public static void method (String msg)
{

}

public static void someOtherMethod(){
    method("calling from someOtherMethod");
}

public static void main (String[] args)
{
    try {
        method("calling from main");
    }
    catch (Exception exc) {
        method("an exception is thrown" );
    }

   someOtherMethod(){
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM