exception handling - Java - Why java.lang.RuntimeException or sub-class of it do not force us to write code in try/catch -
this question has answer here:
why java.lang.runtimeexception (or sub-class of it) not force write code in try/catch
provided:
java.lang.exception or sub-class of (termed checked-exception) forces write code in try/catch block or forces handle it.
then why java.lang.runtimeexception or sub-class (termed unchecked-exception) not force write code in try/catch block if extending java.lang.exception
added example:
public class exceptiontest { public static void main(string[] args) { // how compiler come know or decide method throwing runtimeexception or exception (keep in mind java.lang.runtimeexception again extending java.lang.exception) new exceptiontest().test_1(); } public void test_1() throws myexception { } } class myruntimeruntimeexception extends runtimeexception { public myruntimeruntimeexception() { // todo auto-generated constructor stub } } class myexception extends exception { public myexception() { // todo auto-generated constructor stub } }
this part of design o java language. runtimeexceptions intended exceptions represents errors on behalf of programmer , called unchecked exceptions. other exceptions checked exceptions , intent programmers should have handle them explicitly either catching them or declaring them throws declaration on method call let callers know thrown.
Comments
Post a Comment