[英]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.