somebody please help me understand the difference between checked exceptions & unchecked exceptions. why checked exceptions only need be declared in a java program & not the runtime exceptions? any input will be appreciated regards nitin
Maybe this helps: I think of checked exceptions as exceptions that are somehow expected. E.g. when opening a file, the file may not exist. In this case, the program should offer some alternative. Likewise I take unchecked exceptions as bugs and should be avoided programmatically.
posted 16 years ago
Hmmmm, an unchecked exception is not really a bug in my opinion. Take the following example. You ask the user to fill in some integer value so that you can perform the following operation 10/user_value. Now if the user provides a 0, the expression is going to throw an ArithmeticException. It is not really a bug, you could either catch that exception and inform the user that 0 is not acceptable or you can develop a input parser that verifies that the user does not enter 0 in the first place. Either way is valid. Runtime exception are exception whose cause is difficult to prove at compile-time... (JLS 11.2.2 Why Runtime Exceptions are Not Checked)
Yeah, that's true. And a good explanation in JLS. Thank you.
posted 16 years ago
hi valentine, i've changed my publicly displayed name. thanx to u & mag for the clarifications. still the difference is vague after my reading the JLS also. wht JLS says is --- This compile-time checking for the presence of exception handlers is designed to reduce the number of exceptions which are not properly handled.(abt chkd exceptions) The information available to a compiler, and the level of analysis the compiler performs, are usually not sufficient to establish that such run-time exceptions cannot occur, even though this may be obvious to the programmer. Requiring such exception classes to be declared would simply be an irritation to programmers.(abt unchkd exceptions) my question is if language implementers wanted to allow programmers to handle exceptional situations by means of handler code, then why the runtime exceptions shud not be also declared in the method definitions & handled by code as the case of chkd exceptions, bcos the possibility of occurrence of chkd exceptions is same as that of runtime exceptions. Regards