简体   繁体   English

从catch块调用方法

[英]Calling a method from catch block

I just want to know what will happen when i call a method from catch block ,which is responsible for throwing the exception(Throws the exception). 我只想知道当我从catch块调用方法时会发生什么,该方法负责引发异常(引发异常)。 I searched hard about it but no any satisfiable answer found.... 我对此进行了艰苦的搜索,但未找到任何令人满意的答案。

for eg. 例如

public void A()
{
 try{
     //code which may throw exception
    }
 catch(Exception e)
  {
   A();
  }
}

Did you try it? 你试过了吗? Calling the method there does the same as calling it anywhere. 在那里调用方法与在任何地方调用方法相同。 If it returns a value, throws an exception, halts the JVM, or whatever, it'll work there just like anywhere. 如果它返回一个值,引发异常,中止JVM或其他操作,它将在任何地方都可以正常工作。 What you're doing is some form of recursion , where a method invokes itself. 您正在做的是某种形式的递归 ,即方法调用自身。 When you do that, you have to have some kind of terminating condition , or it'll go on invoking itself forever or, since you're in Java, until you run out of stack space, when you'll get a StackOverflowError . 执行此操作时,您必须具有某种终止条件 ,否则它将永远调用自身,或者由于您使用的是Java,直到堆栈空间用尽时,您会收到StackOverflowError

Why would you like to call the same method in the catch block in which the exception is thrown? 您为什么要在引发异常的catch块中调用相同的方法?

It it keeps on throwing exceptions, then it may result in an infinite loop causing java heap space error. 它不断抛出异常,然后可能导致无限循环,从而导致Java堆空间错误。

You should call a method that handles that exception or takes some appropriate steps for exception. 您应该调用处理该异常的方法,或者采取一些适当的步骤来处理异常。

Now for your question, you can call any method that is accessible inside your method A() inside the catch block. 现在,对于您的问题,您可以调用catch块中方法A()可访问的任何方法。

public void myMethod(){

}
public void A()
{
 try{
     //code which may throw exception
 }
 catch(Exception e)
 {
   myMethod();
 }
}

是的,代码将陷入无限循环中,并一次又一次抛出异常。

//Just add like while condition  in your method 
int count = 0;
int max-attempt = 5;
while(true) {
    try {
        // Some Code
        // break out of loop, or return, on success
    } catch (Exception e) {
        // handle exception
        if (++count >= max-attempt) throw e;
    }
}

This will cause an infinite loop. 这将导致无限循环。 With this code: 使用此代码:

package com.sandbox;

public class Sandbox {

    public static void main(String[] args) {
        new Sandbox().run();
    }

    private void run() {
        try {
            throw new RuntimeException("A");
        } catch (Exception e) {
            run();
        }
    }
}

This is the exception thrown: 这是引发的异常:

Exception in thread "main" java.lang.StackOverflowError
at java.lang.Exception.<init>(Exception.java:66)
at java.lang.RuntimeException.<init>(RuntimeException.java:62)
at com.sandbox.Sandbox.run(Sandbox.java:11)
at com.sandbox.Sandbox.run(Sandbox.java:13)
at com.sandbox.Sandbox.run(Sandbox.java:13)
at com.sandbox.Sandbox.run(Sandbox.java:13)

It will re-try the method A() 它将重试方法A()

However, Till the Exception is not-occured this will keep on re-trying. 但是,直到未发生异常,这将继续重试。

Unless Exception in this scenario is temporary one , this might cause an infinite loop. 除非在这种情况下Exception是临时的,否则可能会导致无限循环。

probably the best way to do is issue a A() call on some condition / timeout. 最好的方法可能是在某些条件/超时下发出A()调用。

You must Call a method in catch block iff you want that method to perform a specific task when an Exception is caught /thrown . 如果要捕获/抛出Exception时希望该方法执行特定任务,则必须在catch块中调用一个方法。 Example : you may want to print the reason for an exception occured ;) 示例:您可能希望打印出现异常的原因;)

I had a similar requirement and tried this 我有类似的要求并尝试过

public void A(Boolean callingFromCatch)
{
if(!callingFromCatch){
 try{
     //code which may throw exception
    }
 catch(Exception e)
  {
   A(true);
  }
}else{
     try{
         reloadPage();
         //try again
        }
        catch(Exception e){}
    }
}

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

相关问题 在try块中发生异常时,从方法调用catch块 - calling catch block from a method when an exception occured in try block 从方法,“try”块或“catch”块后返回? - Return from method, in the “try” block or after “catch” block? 从catch块调用@afterThrowing建议来打印抛出的异常 - Calling @afterThrowing advice from catch block for printing thrown exception 如何制作一个try-catch块,该块不断调用对象上的方法,直到没有更多的异常可以捕获为止 - How to make a try-catch block that keeps on calling a method on an object until there are no more exceptions to catch 如何在Java中的catch块中调用方法? - How do you call a method from a catch block in java? 无法从Web服务方法的try catch块访问参数 - Cannot access a parameter from try catch block of a web service method 从catch块的结果调用带有泛型参数的Java方法 - Java method with Generic argument called from result of catch block 在方法内尝试/捕获块 - Try/catch block inside method 为什么我的String在try / catch块之后更改为旧的输入表单扫描仪(编辑:再次调用方法) - Why my String changes to an old input form Scanner after a try/catch block (edit: calling method again) 即使在catch块中捕获了异常对象之后,我们还能将异常对象扔给调用方法吗? - can we throw the exception object to calling method even after catching it in the catch block?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM