• Post Reply Bookmark Topic Watch Topic
  • New Topic

Checked vs Unchecked Excpetions  RSS feed

 
Ioanna Katsanou
Ranch Hand
Posts: 131
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I can't seem to clearly understand the difference between checked and unchecked exceptions.

According to SYBEX Certified Associate Java® SE 8 Programmer I, Study Guide, Exam 1Z0-808 page 302-303

A runtime exception is defi ned as the RuntimeException class and its subclasses. Runtime exceptions tend to be unexpected but not necessarily fatal. For example, accessing an invalid array index is unexpected. Runtime exceptions are also known as unchecked exceptions. A checked exception includes Exception and all subclasses that do not extend RuntimeException. Checked exceptions tend to be more anticipated—for example, trying to read a file that doesn’t exist.


I can't seem to understand the difference between checked and unchecked exceptions. Don't all exception occur at runtime??
For checked exceptions, Java requires the code to either handle them or declare them in the method signature. An example of a runtime exception is a NullPointerException, which happens when you try to call a member on a null reference.
why is null pointer a runtime exception?
In my experience usually we handle a null pointer exception. In this logic, why isn't  a null pointer a checked exception?

Thanks
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use the enter key repeatedly in normal text; let the user's browser organise the line lengths.

Have you read the Java™ Tutorials section about exceptions? The word RuntimeException was chosen because the exception occurs entirely inside the runtime, whereas other exceptions occur somewhere else. For example an IOException can occur because the file is read‑only, or a network cable is disconnected or similar. The theory is that things going wrong in the runtime cannot be corrected and might not be predictable, so why shou‍ld you catch them. On the other hand checked exceptions might allow one to correct them so the compiler forces one to handle them.

That is the theory. There are some runtime exceptions which can easily be corrected, e.g. NumberFormatException, but most, including NullPointerException, would require changing the code to sort them out. You shoul‍ld not usually catch an NPE, but work out why it happened and correct the code. As I said, that is the theory, but Java® exceptions is a notoriously controversial subject.
Does the book say anything about Throwable or Error, and whether they are checked or unchecked?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:As I said, that is the theory, but Java® exceptions is a notoriously controversial subject.
Does the book say anything about Throwable or Error, and whether they are checked or unchecked?


In my opinion, the whole concept of checked exceptions is controversial. I believe this is why C# didn't bother supporting it.

Henry
 
Ioanna Katsanou
Ranch Hand
Posts: 131
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Don't use the enter key repeatedly in normal text; let the user's browser organise the line lengths.


Does the book say anything about Throwable or Error, and whether they are checked or unchecked?


It does mention them, but it does not specify if they are checked or unchecked exceptions.
It just says that a checked exception includes Exception and all subclasses that do not extend
RuntimeException.
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case have a look at the links provided and see what it says there.
As Henry says, there is a great deal of controversy about checked exceptions and he is surely right about C#.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!