Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

checked & unchecked exceptions...

 
pal kandy
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pal k,
The following discussions might be helpful:
http://www.coderanch.com/t/237185/java-programmer-SCJP/certification/Checked-Exception
http://www.coderanch.com/t/193763/java-programmer-SCJP/certification/Unchecked-Checked-Exceptions
http://www.coderanch.com/t/208417/java-programmer-SCJP/certification/checked-unchecked-exceptions
I got those links by running a search in this forum with the keywords "checked unchecked", I got 88 results!!
Moreover, I'd like you to read the Javaranch Naming Policy and change your publicly displayed name to comply with our unique rule. Thank you.
[ April 12, 2002: Message edited by: Valentin Crettaz ]
 
Mag Hoehme
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Mag Hoehme
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, that's true. And a good explanation in JLS.
Thank you.
 
pal kandy
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic