• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is Error an unchecked exception?

 
Tonguc Koc
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I'd like to ask you about a terminology confusion which I've faced recently. I hope you wouldn't mind about this basic "wording" stuff.

Q7-6. Select the incorrect statement(s):
a java.lang.Throwable is the base class of all type of exceptions.
b If a class is a subclass of java.lang.Exception, it may or may not be a checked
exception.
c Error is an unchecked exception.
d Error and checked exceptions need not be part of a method signature.

Is Error an unchecked exception? In Java Tutorial page I see the following explanation in bold, which makes me to think that an Error is unchecked exception. On the other hand I know that Error is a subclass of Throwable and doesn't inherit from Exception. Or should we say Error is an unchecked Throwable?

From Java Tutorials (http://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html)
Because the Java programming language does not require methods to catch or to specify unchecked exceptions (RuntimeException, Error, and their subclasses), programmers may be tempted to write code that throws only unchecked exceptions or to make all their exception subclasses inherit from RuntimeException. Both of these shortcuts allow programmers to write code without bothering with compiler errors and without bothering to specify or to catch any exceptions. Although this may seem convenient to the programmer, it sidesteps the intent of the catch or specify requirement and can cause problems for others using your classes.

Another resource which causes confusion taken from an arbitrary website:


 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or should we say Error is an unchecked Throwable?

This is correct. Error is in fact unchecked. But it isn't an exception.

The difference between unchecked exceptions and "unchecked throwables" is that your code shouldn't throw an error. Errors are things like running out of memory that your program can't possibly recover from. Making it so there is no disagreement whether they should be checked or unchecked.
 
Tonguc Koc
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeanne,

I was checking the JLS and found the following:

http://docs.oracle.com/javase/specs/jls/se7/html/jls-11.html

"The unchecked exception classes are the run-time exception classes and the error classes.

The checked exception classes are all exception classes other than the unchecked exception classes. That is, the checked exception classes are all subclasses of Throwable other than RuntimeException and its subclasses and Error and its subclasses."

So isn't an error a unchecked exception by this definition? (Getting more confused...)
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. I didn't look at the spec. They are using "exception" to mean something different than "Exception."
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tonguc Koc wrote:So isn't an error a unchecked exception by this definition? (Getting more confused...)

Don't be confused! It's easy: all runtime exeptions (extending RuntimeException) and all errors (extending Error) are unchecked exceptions. All the other ones are checked exceptions (and thus should be handled or declared).
 
Tonguc Koc
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I'm pretty confident on what should be "caught or declared" in code questions, however these type of "wording" issues are really wasting my energy. You never know what the author has in his/her mind

For example for the question concerned, the answer in book is C&D, which I went for only D.

Q7-6. Select the incorrect statement(s):
a java.lang.Throwable is the base class of all type of exceptions.
b If a class is a subclass of java.lang.Exception, it may or may not be a checked
exception.
c Error is an unchecked exception.
d Error and checked exceptions need not be part of a method signature.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Error is definitely considered as an unchecked exception, so the answer in the book is wrong!

(note: on the exam you'll know for each question how much answers you have to select)
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which book has this question? (always QuoteYourSources)
 
Tonguc Koc
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roel,

I'll keep with your answer as the JLS also uses the same terminology.

The book is Mala Gupta's OCA Java SE7 Certification guide.

Answer: c, d
Explanation:
Option (a) is a true statement. A checked exception is a subclass of java.lang
.Exception, and a runtime exception is a subclass of java.lang.RuntimeException.
java.lang.RuntimeException is a subclass of java.lang.Exception, and java.lang
.Exception is a subclass of java.lang.Throwable. Hence, all the exceptions are subclasses
of java.lang.Throwable.
Option (b) is also a true statement. Unchecked exceptions are subclasses of class
java.lang.RuntimeException, which itself is a subclass of java.lang.Exception.
Hence, a class can be a subclass of class java.lang.Exception and either a checked
or an unchecked exception.
Option (c) is a false statement. Error is not an exception. It does not subclass
java.lang.Exception.
Option (d) is also a false statement. Error need not be part of a method signature,
but checked exceptions must be a part of the method signatures.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tonguc,
I was the technical proofreader for that book. I missed the distinction between "exception" (concept) and "Exception" (class name) when proofreading just like I did in this thread.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tonguc Koc wrote:The book is Mala Gupta's OCA Java SE7 Certification guide.

I notified the author of the book about this erratum.
 
N Sam
Ranch Hand
Posts: 77
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to revive this thread, because i did not think option 'd' is incorrect. The author says in the explanation that checked exceptions must be a part of the method signatures. They 'can' be, but not 'must be' ; Atleast that is what i understand from reading this book. (page 401) Please explain why option 'd' is incorrect. Thanks.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
N Sam wrote:Please explain why option 'd' is incorrect.

If a method can throw a checked exception, it must be declared in the method signature. If it doesn't (like option d suggests, your code won't compile)
 
N Sam
Ranch Hand
Posts: 77
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But there is nothing in option 'd' to indicate that the method in question is indeed 'throwing' an exception. I agree that 'if it throws, it must be in signature'. Here is option 'd' again:

Error and checked exceptions need not be part of a method signature.

This is indeed a correct statement, in case of a method that does not throw any exceptions, but simply catches and handles them inside a try/catch block. I am considering cases shown in the book, that show a method signature (without exception ) for a method that simply shows a try/catch block inside it. It has been already proven that there is one error in this question, so i am bound to think 'why not another error in option d'.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
N Sam wrote:But there is nothing in option 'd' to indicate that the method in question is indeed 'throwing' an exception.

Fair comment! Creating unambiguous questions (and answers) is one of the hardest things when writing a certification study guide. I'll pass this one also to the author of the book.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic