0
public class StackTest {
    public static void main(String[] args) {
        show();
        System.out.print("welcome back to maain");
        display();
    }
    static void show(){
        try{
            show();    //recursion
        }catch(StackOverflowError e){
            System.out.print("error cought");
        }
    }
    static void display(){
        System.out.print("after stack overflow error");
    }
}

In this program an StackOverflowError occurs but gets handled and the program does not terminated abnormally. why? You can see this at http://ideone.com/vwSav

dacwe
  • 43,066
  • 12
  • 116
  • 140
Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117
  • 1
    related: http://stackoverflow.com/questions/2535723/try-catch-on-stack-overflows-in-java – Thilo Aug 30 '11 at 06:23

5 Answers5

5

You can handle Errors because they are Throwable just like Exceptions.

Errors are designed to indicate problems outside your program's control, like OutOfMemoryError and StackOverflowError, but you can define your own errors, too.

Perhaps you are thinking that, or heard that, OutOFMemoryError can be caught but there's no guarantee you'll have enough space to execute the handler, so errors must in general not be something you can catch. In your case, though, you got away with it. No language rules were violated in the catching and handling of this error.

The real question is, should you catch them? Normally when an error, as opposed to an exception, is thrown, your application is very likely in an inconsistent state, making recovery a crapshoot at best. So be really, really careful. Better to forget it and let the app die though, since whatever is running after the handler is not guaranteed to be something you'd want to run.

Ray Toal
  • 86,166
  • 18
  • 182
  • 232
  • so you confirm that an `Error` could be caught? – Mohammad Faisal Aug 30 '11 at 06:50
  • @Mohammad Faisal - that's what he said. And that's what your code does. Errors CAN be caught ... but it is usually a BAD IDEA to do this. (Like firing a gun at your foot is usually a bad idea.) – Stephen C Aug 30 '11 at 07:00
  • @Mohammad, yes indeed `Error` objects can be caught. If you want confirmation, see [the section on try statements at the Java Language Specification](http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.20). It says at this link that the catch clause takes parameters whose declared types are `Throwable` or any subclass of `Throwable`. Note that `Error` is a subclass of `Throwable`. So again, possible but not advised. – Ray Toal Aug 30 '11 at 07:11
2

Why would you expect it to terminate when you catch the exception (or rather the Error in this case)? What else would that catch block do?

You can catch and handle pretty much all error conditions, though usually you should only catch Exceptions.

Thilo
  • 257,207
  • 101
  • 511
  • 656
1

You can catch any Throwable and it is up to the developer to handle it correctly. You can even handle ThreadDeath (triggered by a Thread.stop()) or another sub-class of Throwable (which is neither an Error or an Exception)

public class MyThrowable extends Throwable { } // checked "exception"

try {
   throw new MyThrowable();
} catch (Throwable t) {
   t.printStackTrace();
   Thread.currentThread().stop(t); // rethrow blindly.
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Are there examples of Throwables that are neither Error nor Exception? – Thilo Aug 30 '11 at 06:48
  • I have added an example. IMHO, Its not good programming practice because its obscure and most people won't know why you are not using an Exception or Error. ;) – Peter Lawrey Aug 30 '11 at 07:12
  • 1
    I was thinking "examples in the wild". Probably not, because it would be frowned upon. – Thilo Aug 30 '11 at 09:01
0

It will only be terminated abnormally if your exception is propagated all the way up to your main method and you don't handle it there. Usually happens for unchecked run time exceptions. If you want to terminate your program and shut down the VM you can call System.exit(int errorCode) in the catch block, there will be programmers which always complain here if that's done but that's a way to do it.

0

usually you don't catch Error, except for LinkageErrors, no class def found errors, unsatisfied link errors, incompatible class change errors..

also an outofmemory error (sometimes the stackoverflow exception) doesnot give control the catch block as there is no memory.

Anantha Sharma
  • 9,920
  • 4
  • 33
  • 35