• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Checked vs Unchecked Excpetions

 
Ranch Hand
Posts: 166
1
Oracle Java
  • 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
 
Marshal
Posts: 67444
257
  • 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?
 
author
Posts: 23853
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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: 166
1
Oracle Java
  • 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: 67444
257
  • 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#.
 
"Don't believe every tiny ad you see on the internet. But this one is rock solid." - George Washington
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!