• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Little error on page 312 in Boyarsky and Selikoff book about exceptions?

 
Ben Pittens
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

In the book Boyarsky and Selikoff I found something I think it is not completely correct.

In the code example look like this.



I think this code doesn't compile because in the finaly clause an checked exception is thrown and it is not handled.
Nevertheless because de method declaration is not shown we can't see if the method might throw a exception.
Is that correct?

I Didn't find this on de eratta list of the book.Erratta list book

Kind regards
Ben
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
some things about your code and post
# finally must have curly braces {
# you are not throwing a checked Exception in finally but you are throwing a Exception. Exception is the superclass that is extended by RuntimeExceptions(Unchecked Exceptions) and other Checked Exceptions.
if you see in the book, the code line numbers start from 26,so I guess we must assume that code snippet must be inside another code block, and it simply has a throws Exception like this.
subsequently they also talk about try/catch in the finally block.Check page 16 "code Formatting on the Exam" where it talks about line numbers. Though it is for imports, I guess it applies to all.
 
Ben Pittens
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ramya,

Thanks, Ok, yes I allready mentioned that about the fact that the code is not complete, some parts are not displayed.
And te curly braces was a typo.

Kind regards
Ben
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Pittens wrote:I think this code doesn't compile because in the finaly clause an checked exception is thrown and it is not handled.
Nevertheless because de method declaration is not shown we can't see if the method might throw a exception.
Is that correct?

No, that's incorrect! The code snippet as-is compiles successfully (I fixed your "curly brace" typo). There is no invalid Java syntax. A finally clause can throw a checked exception, unchecked exception or even an error. So the compiler won't complain. But this is just a snippet, so if you add this snippet inside a method you have of course to handle or declare the checked exception thrown by the finally clause. Otherwise you'll get a compiler error (as you have noticed), but that's not related to this code snippet.

I like to add two notes:
1/ expect on the exam to see small code snippets like this one. If "Compilation fails" is one of the answer options, you have to focus only on the code you see in order to determine if there is a compiler error or not. You can assume that all necessary supporting code exists is present and compiles successfully. This is actually one of the assumptions which Oracle recently added to the exam topics of the OCA certification exam.
2/ when the finally clause doesn't end normally (like in this code snippet), strange things can (and probably will) happen. You'll find excellent explanations with code snippets in this thread and this one. Both are definitely worth reading!

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic