Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confused about exception handler, p. 337 (K&B7)

 
Mark Kevin
Ranch Hand
Posts: 44
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The middle of the last paragraph on page 337 reads in part, "... execution transfers to the first line of that exception handler, line 8 in the catch clause." But shouldn't that be line 7 instead? Here is the snippet.


Before the first statement runs in the block, the thrown exception, an object, is caught by the variable in the catch, which is line 7, ie., catch(Exception e) {}. It's not like the try block where execution begins with the first line inside the block, or the finally block.

Contrast the book's example with a similar one on page 336 which, referring to the code on page 335, states, "However, if at any time in lines 2 through 5 (the try block) an exception of type MyFirstException is thrown, execution will immediately transfer to line 7" (which appears to be correct). Line 7 is the following:



These two statements conflict. In other words, on page 337 execution should transfer to line 7, not line 8. Otherwise there is a contradiction between p. 337 and p. 336. Or maybe it's wrong on page 336. I don't know.

Or maybe I don't know what I'm talking about.

 
Les Morgan
Rancher
Posts: 711
17
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is more of a wording problem than an error or error in the example:

"... execution transfers to the first line of that exception handler"

The catch statement has to be evaluated to decide what exception handler to transfer execution to, but the code between the catch block is/may be considered the exception handler while ignoring the fact that the catch statement had to be eveluated.

"an exception of type MyFirstException is thrown, execution will immediately transfer to line 7"

write some code and see the execution of what happens when an exception is thrown, you do go to the catch to evaluate it, so execution is transferred immediately to the catch statement to be evaluated.

Notice the difference--the word "immediately", good catch, but I doubt the author will say it is an error in example due to the difference in the wording.
 
Mark Kevin
Ranch Hand
Posts: 44
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks, Les, although it seems as though in both cases execution transfers immediately - coming from the try block - so that word shouldn't control. What seems to me to matter more is where control immediately lands from the try, which is the catch() - a mandatory step in the execution of the catch block.

I also thought catch(Exception e) did more than evaluate which exception was thrown, but was also used to execute, for instance, a message, using variable e as the object reference, ie., e.printAMessage();

As it's written it leaves the impression that line 7 is bypassed when the code in the block runs, which it isn't. To me. I think the authors should take a closer look at the language. Just trying to help. And pass that OCA and OCP exam! Oh, my.

Onward.

 
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
Les Morgan wrote:write some code and see the execution of what happens when an exception is thrown, you do go to the catch to evaluate it, so execution is transferred immediately to the catch statement to be evaluated.

That's what I actually did And I was pretty surprised to see that Eclipse has different behavior depending if you have one or more than one catch handler.

The first code snippet has just one catch handlerI put two breakpoints: one on the catch handler itself (line1) and another one on the statement in the catch handler (line2). And when I debugged the program, the program was suspended on line2 (and thus not on line1).

Now in the second code snippet I added another catch handlerAnd again I added a breakpoint on both line1 and line2. When I debugged this code snippet, the program was suspended two times: first on line1 and then on line2. Then I moved the catch handler of IllegalStateException beneath the catch handler of NullPointerException and I was (again) surprised to see the same behavior as the first code snippet (execution suspends immediately on line2).

Disclaimer: this is of course the behavior of one IDE. Another IDE might have a different (and more consistent) behavior. So this behavior should not be considered as how execution flow should happen.

Hope it helps!
Kind regards,
Roel
 
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
Mark Kevin wrote:Thanks, Les, although it seems as though in both cases execution transfers immediately - coming from the try block - so that word shouldn't control. What seems to me to matter more is where control immediately lands from the try, which is the catch() - a mandatory step in the execution of the catch block.

Consider the catch block as the statements you want to be executed when this exception (or one of its subclasses if there's no catch handler for that particular subclass) is thrown by any line of code in the try block. When this exception occurs in the try block, the execution of the code will continue with the first statement in the catch block for this exception.
The line of the catch block itself could/should be considered as NOOP. It's only an indication of which exceptions will be handled by that catch block. As far as I know you can't add any statement to that line to do something useful (like printing a message). Of course I know you can write something likebut that's cheating and doesn't count
 
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
Mark Kevin wrote:These two statements conflict. In other words, on page 337 execution should transfer to line 7, not line 8. Otherwise there is a contradiction between p. 337 and p. 336. Or maybe it's wrong on page 336. I don't know.

I wonder if it's worth adding it to the errata overview, because it seems to be a very minor issue, just a slight difference in wording. Anyway my suggestion would be to change page 337, something like "execution transfers to that exception handler (line 7 in the code)"; then both statements are more or less equivalent.
 
Mark Kevin
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The line of the catch block itself could/should be considered as NOOP. It's only an indication of which exceptions will be handled by that catch block. As far as I know you can't add any statement to that line to do something useful (like printing a message).


I went back and reviewed my textbook. In a catch clause such as catch(Exception e), the e is simply a variable for type Exception. It catches the thrown object and if it is the first matching object of said type, then the code block is executed. As such, I always regarded the part "Exception e" as a statement. That's what I learned in the book. It's a declared method variable similar to, say, an instance variable like "private String words;

When the exception is thrown, it is caught by the variable e which initializes that variable with that thrown object of type Exception. So it is a mandatory step, a statement, which must be executed (initialized) before the block of code is entered. I think. Of course you don't add anything to that e variable within the method (between the parentheses) but that object reference e can be used in the block to display the message (e.getMessage()) or you can override its toString() and create your own message (or do other things).

The main point being that even if catch(Exception e) is merely used to evaluate whether there is a match, the thrown object still has to land in the variable e which initializes it, which, it seems to me, is executing a statement just like any other variable declaration and initialization. Line 7, not line 8 begins the control and execution. Just my two cents.
 
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
Mark Kevin wrote:As such, I always regarded the part "Exception e" as a statement. That's what I learned in the book. It's a declared method variable similar to, say, an instance variable like "private String words;

For the JLS Exception e is not a statement, but a part of the try statement. But you are correct: you could compare Exception e with a method parameter. If you invoke a method, you are required to pass a value for the method parameter and thus the parameter will always be assigned with this value. The reference variable e will be assigned with the thrown exception from the try block. So there's definitely some action happening here. So the NOOP from my previous post was a poor choice of words, although I was refering to the fact you can't add System.out.print(e.getMessage()); to the Exception e part itself.

Mark Kevin wrote:Of course you don't add anything to that e variable within the method (between the parentheses) but that object reference e can be used in the block to display the message (e.getMessage()) or you can override its toString() and create your own message (or do other things).

I can agree with the first part of this sentence: using the reference variable e you could display the message in the catch block using e.getMessage(). But the second part is incorrect: overriding the toString() method can't be done using the reference variable e, you can only override the toString() method in the exception class itself.

Mark Kevin wrote:Line 7, not line 8 begins the control and execution.

That's why I suggested changing page 337 (execution transfers to line 7).
 
Mark Kevin
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

As always, thanks for the explanation.

m.
 
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
Roel De Nijs wrote:Anyway my suggestion would be to change page 337, something like "execution transfers to that exception handler (line 7 in the code)"; then both statements are more or less equivalent.

Added to the errata overview.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic