Forgive me if this is a stupid question, but as far as I know, all Java exceptions should be caught and handled. For example, something like this would create a compiler error:
public String foo(Object o) { if (o instanceof Boolean) { throw new Exception(); } return o.toString(); }
Since the foo() method did not add a throws .
However, this example will work (if none of the foo() methods had a throws or the bar() method did not surround using foo() in a try/catch ):
public String foo(Object o) throws Exception { if (o instanceof Boolean) { throw new Exception(); } return o.toString(); } public void bar(Object o) { try { String s = foo(o); } catch (Exception e) {
In the end, sometimes a Java program still crashes due to an unhandled exception.
How does this happen?
source share