简体   繁体   English

异常没有抛出Java

[英]Exception not thrown Java

class ex1
{
    static void my() {
        System.out.println("asdsdf");
    }

    public static void main(String args[]) {

        try {
            for (;;) {
                my();
            }
        } catch (Exception e)//Exception is not caught //Line 1
        {
            System.out.println("Overflow caught");
        } finally {
            System.out.println("In Finally");
        }
        System.out.println("After Try Catch Finally...");

    }
}

The catch statement (Line 1) does not handle the overflow exception as such the output keeps on printing "asdsdf" without throwing an exception. catch语句(第1行)不处理溢出异常,因此输出继续打印“asdsdf”而不抛出异常。 Can anyone tell me why an infinite loop is not handled as an exception ?. 任何人都可以告诉我为什么无限循环不作为例外处理? Or that's the way it's designed and supposed to work ? 或者这就是它的设计和应该工作的方式?

An exception is not caught because it is never thrown. 不会捕获异常,因为它永远不会被抛出。 Your method does nothing to cause an OverflowException. 您的方法不会导致OverflowException。

An infinite loop is perfectly legal in Java. 无限循环在Java中是完全合法的。 It will continue running indefinitely. 它将继续无限期地运行。 Your loop is also not building more and more resources, it is simply calling a single method which self destructs every iteration after printing to the standard output. 你的循环也没有构建越来越多的资源,它只是调用一个方法,在打印到标准输出后自我破坏每次迭代。 It could run forever. 它可以永远运行。

If you, for example, had the method my(); 例如,如果你有方法my(); ITSELF simply call my() , then you would immediately get a StackOverflowError , but this would happen on the very first iteration of your for(;;) loop. ITSELF只是调用my() ,然后你会立即得到一个StackOverflowError ,但这会在你的for(;;)循环的第一次迭代中发生。

To create an "overflow" condition, you actually have to cause something to overflow. 要创建“溢出”条件,您实际上必须导致某些内容溢出。 Like a variable. 像一个变量。

Modify your for statement to increment something but do not place any constraint on the continuity of the loop, then there would be an integer overflow. 修改你的for语句以增加某些东西但不对循环的连续性施加任何约束,然后会出现整数溢出。

for (int i=0;;i++) {
}

Alternatively, 或者,

for (int i=0;i==i;i++) { // i==i is always true.
}

Another way is to cause a call stack overflow, by recursively calling itself without limit. 另一种方法是通过递归调用自身而无限制地引起调用堆栈溢出。 Each recursive call has to preserve the stack of the preceding recursive call. 每个递归调用都必须保留前面的递归调用的堆栈。

Recursive function : 递归函数

public static my(){
  my();
}

Recursive constructor : 递归构造函数

class My {
  My my;
  My() {
     try{
       my = new My();
     }
     catch (Exception e){
        // print exception.
     }
  }
}

That's the way it's designed and intended to work - there's this thing called the Halting problem which basically means that would be impossible. 这就是它设计和打算工作的方式 - 这就是所谓的停机问题 ,基本上意味着这是不可能的。

If on the other hand your method was recursive it would consume more and more stack space until an exception was thrown. 另一方面,如果你的方法是递归的,它将消耗越来越多的堆栈空间,直到抛出异常。

Your method just creates an infinite loop and calls a method . 您的方法只是创建一个无限循环并调用方法。 Since no exceptions are thrown you aren't seeing any . 由于没有抛出异常,你没有看到任何异常。

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

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