在java中尝试/捕获堆栈溢出?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2535723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
try/catch on stack overflows in java?
提问by stereos
Can you try/catch a stack overflow exception in java? It seems to be throwing itself either way. When my procedures overflows, I'd like to "penalize" that value.
你能在java中尝试/捕获堆栈溢出异常吗?它似乎在以任何一种方式投掷自己。当我的程序溢出时,我想“惩罚”那个值。
采纳答案by Thilo
Seems to work:
似乎工作:
public class Test {
public static void main(String[] argv){
try{
main(null);
}
catch(StackOverflowError e){
System.err.println("ouch!");
}
}
}
回答by Michael Aaron Safyan
If you are getting a stack overflow, you are likely attempting infinite recursion or are severely abusing function invocations. Perhaps you might consider making some of your procedures iterative instead of recursive or double-check that you have a correct base case in your recursive procedure. Catching a stack overflow exception is a bad idea; you are treating the symptoms without addressing the underlying cause.
如果您遇到堆栈溢出,您可能正在尝试无限递归或严重滥用函数调用。也许您可能会考虑使您的某些过程迭代而不是递归或仔细检查您的递归过程中是否有正确的基本情况。捕获堆栈溢出异常是个坏主意;您正在治疗症状而不解决根本原因。
回答by Slava Imeshev
I agree with Michael - StackOverflowException is a signal that something went very wrong. Swallowing it is not a good idea. The best course of action is to fix the root cause of this error.
我同意 Michael - StackOverflowException 是出现问题的信号。吞下它不是一个好主意。最好的做法是修复此错误的根本原因。
回答by Daniel
You have to catch an Error, not the Exception
您必须捕获错误,而不是异常
回答by Gangnus
The functional features of Java 8 makes this question incomparably more important. For while we start to use recursion massively, StackOverflowException is something we MUST count for.
Java 8 的功能特性使这个问题变得无比重要。因为当我们开始大量使用递归时,StackOverflowException 是我们必须考虑的事情。
The Java 8 lambdas types has no one among them that throws StackOverflowException. So, we have to create such. It is absolutely necessary, without that we won't pass even the IDE control.
Java 8 lambdas 类型中没有一种会抛出 StackOverflowException。所以,我们必须创建这样的。这是绝对必要的,否则我们甚至不会通过 IDE 控制。
For example, Integer -> Integer function type could look as:
例如,Integer -> Integer 函数类型可能如下所示:
@FunctionalInterface
public interface SoFunction <U> {
public U apply(Integer index) throws StackOverflowException;
}
After that we can write a function that will accept lambdas throwing StackOverflowException.
之后,我们可以编写一个函数来接受抛出 StackOverflowException 的 lambda。
public T get(int currentIndex) throws StackOverflowException{
And only now we can create a recursive lambda:
直到现在我们才能创建一个递归的 lambda:
fiboSequence.setSequenceFunction(
(i) ->
fiboSequence.get(i-2).add(fiboSequence.get(i-1))
);
After that we can call the recursive chain fiboSequence.get(i)
and get a result or a StackOverflowException if the whole chain was incomputable.
之后,我们可以调用递归链fiboSequence.get(i)
并获得结果或如果整个链无法计算则获得 StackOverflowException。
In the case of use of recursion SO gets absolutely different meaning: you have jumped too deep, repeat it dividing in more shallow steps.
在使用递归的情况下,SO 得到完全不同的含义:你跳得太深了,重复它划分更浅的步骤。