• Post Reply Bookmark Topic Watch Topic
  • New Topic

exception question  RSS feed

 
nick woodward
Ranch Hand
Posts: 382
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quick question from a book I'm reading at the moment if anyone can help please. Apologies if the formatting is terrible - I'm on my phone (also the reason why I can't just write the code and double check the answer!)

The question states that given the input a12, "error: no negative" would be printed. I understand why it would print "error" because the first catch statement is more generalised than the InputMismatchException, but why/how does it get the parameter 'no negative'? Surely the exception would have already been thrown?

Apologies, it's been a relatively "hearty" Sunday, no doubt I'm being a bit slow!

Thanks in advance!

 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code does not compile.
InputMismatchException is a subclass of Exception so its catch block needs to be before Exception's catch block.
 
nick woodward
Ranch Hand
Posts: 382
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:Your code does not compile.
InputMismatchException is a subclass of Exception so its catch block needs to be before Exception's catch block.


Hi pawel,

I didn't know it didn't compile, but I'm not surprised - the book is trying to demonstrate that the first catch block makes the second pointless - for the reason you mention. What confuses me is why e.getMessage would be "No Negative" if it did compile. The exception is thrown immediately after scanner.nextInt() and caught by the first catch block, no?

The correct answer to their (admittedly hypothetical) question would be "Error: " only. Not "Error: No negative". Correct?
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
e.getMessage() would return "no negative" because the Exception is created with that message (as the parameter to the constructor). Why do you think it would print a different message?

This obviously ignores the compilation message that has already been mentioned. It also assumes that no RuntimeExceptions or other checked Exceptions are thrown, since a catch block that catches Exception will catch all of them.
 
nick woodward
Ranch Hand
Posts: 382
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi mike,

I guess I'm at a loss as to why that exception would be thrown given a12? It, and its constructor, are never reached are they? Is it not an input mismatch exception that is thrown at line 4, prior to the if condition? That would be caught by the first catch block, printing "error", but not "no negative".

Thanks for the help!
 
Ron McLeod
Bartender
Posts: 1603
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the input a12 or -12?
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I misread your question and thought the input was negative.

Which book is this from?
 
nick woodward
Ranch Hand
Posts: 382
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The input is 'a12' Ron, sorry I should have been more clear. I'm back on my PC now, so that should be easier. I could completely understand the answer if the input was -12. I think it may be a typo, but wanted to check as exceptions aren't my strongest area.

And the book is 'an introduction to object oriented programming with java' by C. Thomas Wu.

It gets a bit of flack in reviews on amazon, but I honestly think its great - maybe as a secondary text, or as a second read through the basics. Reads more like an academic text rather than a 'teach yourself' book, but always straight to the point and I've found it often contains more detail than similar texts.

Thanks again,

Nick
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!