简体   繁体   English

如何找出,如果在java中从catch块调用相同的方法?

[英]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. 我有一个方法,例如void abc(){} ,这个方法直接从我的main()方法调用,也从它的异常块调用,也就是来自catch块。

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). 现在我想找出abc()方法内部,是否正常调用abc() (即从main() )或在异常情况下调用它(即从catch()块调用)。

I tried this , using Thread.getCurrentStackTrace() , however it always returns main() method (as method name), as in both cases method will always remain same. 我尝试使用Thread.getCurrentStackTrace() ,但它总是返回main()方法(作为方法名称),因为在两种情况下方法将始终保持相同。

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. 这里重点是,我无法更改abc()方法的签名,我无法在main()方法或catch()块中设置任何标志,有了这个限制,我试图找出,如何从哪里查看abc()被调用或简单的话....如何在abc()方法中找出是否抛出异常。

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. 使用StackTraceElement ,您可以获得行号,从中可以确定是否从main() catch块调用abc() 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. 注意:仅当您拥有main方法的源代码才能查看行号时,此选项才有效。 Won't work if main is in external jar whose source code is not available to you. 如果main在外部jar中,你的源代码不可用,则无效。

EDIT: Also, this won't work if the external jar is created without line number debug information. 编辑:此外,如果创建没有行号调试信息的外部jar,这将不起作用。

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. Thread.getCurrentStackTrace()显然不起作用,因为无论你是从main调用它还是从catch调用它都会保持相同。

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 比起什么选项,首先你可以做@Eran建议的内容而我不重复那个答案,如果你不想改变方法签名,你可以做的另一件事就是在调用abc之前设置/取消设置一个布尔变量()来自catch block ..见下面的例子

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. 但是,如果需要从trycatch两个函数调用一个函数,那么最好从finally块调用它。 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. 您可以将boolean参数传递给您的方法,以便让它知道调用它的位置。

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. 使用abc()的附加输入,检测方法调用的位置。 If you want to change method abc's functionallity if an error occured use this approach. 如果要在发生错误时更改方法abc的功能,请使用此方法。

  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 . 在类中添加一个静态变量(如布尔值)并在发生错误时设置此变量并在abc检查此变量。

You can add a String parameter to your method: 您可以在方法中添加String参数:

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(){
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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