Exception not thrown Java

java

Solution

An exception is not caught because it is never thrown. Your method does nothing to cause an OverflowException.

An infinite loop is perfectly legal in 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();` ITSELF simply call `my()`, then you would immediately get a `StackOverflowError`, but this would happen on the very first iteration of your `for(;;)` loop.

Problem

``` 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. 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 ?

Original source