Someone please explain the difference between java.lang.RuntimeException and java.lang.Exception? How do I decide which one to extend if I create my own exception?
Someone please explain the difference between java.lang.RuntimeException and java.lang.Exception ? How do I decide
Share
Generally RuntimeExceptions are exceptions that can be prevented programmatically. E.g
NullPointerException,ArrayIndexOutOfBoundException. If you check fornullbefore calling any method,NullPointerExceptionwould never occur. SimilarlyArrayIndexOutOfBoundExceptionwould never occur if you check the index first.RuntimeExceptionare not checked by the compiler, so it is clean code.EDIT : These days people favor
RuntimeExceptionbecause the clean code it produces. It is totally a personal choice.