• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubts on questions #6 and #15 self-test Chapter 6

 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a doubt on two questions of the self-test of Chapter 6 and both questions concern the exceptions to rethrow.

1) Given this code:



I had indicated an error in compile time because I had noticed in the catch block that the exception was rethrows but was not declared in the method signature.
This however is not correct and the solution is "-ic mc mf of".
Is it not correct what I said?


2) Given this code:


and given this four code fragments:


this response:

E. When considering fragments II, III and IV, of those that will compile, adding a try/catch block around line 4 will cause compilation to fail.


is false and I considered false because it is considered the fragment of code IV indicating the possible presence of an unchecked exception but does not require to be declared and even managed.
If it were not for the code snippet IV I would have considered true because if you want to declare AND handle an exception (checked) need to rethrow the exception in the catch block, otherwise there will be an error in compilation.
My reasoning is this correct?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Lerry wrote:I had indicated an error in compile time because I had noticed in the catch block that the exception was rethrows but was not declared in the method signature.
This however is not correct and the solution is "-ic mc mf of".
Is it not correct what I said?

No, it's not correct!

With questions like this one you have to look very carefully to see if every throw statement (of a checked exception) has either a catch block or is declared in the method declaration. And also note that on the actual exam the indentation could be messed up as well. I tweaked the code snippet a bit and added for every throw-statement of a checked exception the throw-statement itself (using lineXa) and the accompanying catch-block (using lineXb).

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
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Lerry wrote:If it were not for the code snippet IV I would have considered true because if you want to declare AND handle an exception (checked) need to rethrow the exception in the catch block, otherwise there will be an error in compilation.
My reasoning is this correct?

No, it's not correct!

As this code snippet demonstrates you can handle and declare the same exception even without rethrowing the exception.And this is true for both checked and unchecked exceptions.

Hope it helps!
Kind regards,
Roel
 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then it is likely that I'm missing the usefulness of declaring an exception. Let's go back a moment back.
If these two pieces of code are both correct




what is the use of declaring an exception?

From what I understood the declaration allows a method to know up front that in it MAY be throw an exception. Once raised then you may want to manage it in the same way (try / catch) or handle it within the method where the method "indicted" is invoked.
Simply put, when I have to declare AND handle the exception? and when I have to declare OR manage it?

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Lerry wrote:Simply put, when I have to declare AND handle the exception? and when I have to declare OR manage it?

Checked exceptions MUST be handled OR declared. Always! If you don't do it, the compiler will punish you and will not compile your program successfully.

But you might handle AND declare a checked exception at any time (could be a business requirement). But it's not required at all to handle AND declare. So if you don't do it, the compiler will definitely not complain. But if you need to, you can definitely do it. But most of the time you will opt for one or the other, not for one and the other.
 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
then these pieces of code:













are ALL correct?
 
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
John Lerry wrote:are ALL correct?

Almost! 3 out of 4 are indeed valid methods which will compile successfully But one code snippet fails to meet the declare-or-handle rule, so this one won't compile. Can you spot which one? Remember: if you re-throw a checked exception, then for this re-throw the same handle-or-declare rule is required as well.
 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that the code that does not compile is this:




because was re-throws a checked exception but the rule "handle OR declared" is violated because in this case is handle AND declare.
is it correct?

My doubt is due to this phrase in the book (K&B7):

If you throw a checked exception from a catch clause, you must also declare that exception! In other words, you must handle AND declare, as opposed to handle OR declare.

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Lerry wrote:because was re-throws a checked exception but the rule "handle OR declared" is violated because in this case is handle AND declare.
is it correct?

No, it's not correct! Like I said before: handle and declare is allowed.

I tweaked the code snippet a bit and added for every throw-statement of a checked exception the throw-statement itself (using lineXa) and the accompanying catch-block or catch handler (using lineXb).
As you can see: for every throw statement (of a checked exception) you MUST have a catch-block or a throws-clause in the method signature.

John Lerry wrote:My doubt is due to this phrase in the book (K&B7):

If you throw a checked exception from a catch clause, you must also declare that exception! In other words, you must handle AND declare, as opposed to handle OR declare.

That's indeed the statement one of your examples is not adhering to. In one of your methods you rethrow the exception from the catch-clause, but you are not declaring the exception in the method signature. This is nicely illustrated in the previous code snippet: you have 2 throw statements (both of a checked exception) and 1 catch handler (to handle exception thrown on line1a) AND 1 throws-clause in the method signature (to declare exception thrown on line2a) => handle AND declare

So have another close look to your 4 methods and determine again (with this new/improved knowledge) the one that will not compile.

Hope it helps!
Kind regards,
Roel
 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code will not compile is this:


because the exception (the same) is re-throws in the catch block but not declared (in the method signature).

Is it correct?
 
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
John Lerry wrote:because the exception (the same) is re-throws in the catch block but not declared (in the method signature).

Is it correct?

Spot-on!

And what do you think about these methods? Do they compile or not? And if not, why?
I used different method names, so you can easily refer to them by using these method names (e.g. method1 compiles, method2 doesn't compile because... and so on)

Good luck!
Kind regards,
Roel
 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


compile.



does not compile because the IOException is not handled.



compile. Even if the IllegalArgumentException is not handled as it is a subclass of RuntimeException that exception can not be handled.



compile.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done! Almost a perfect score. 3 out of 4 correct and all the explanations are spot-on as well! The one you missed was a (very) tricky question

The correct ones:
  • method1: compile
  • method2: does not compile (the checked IOException is not handled and it's not declared)
  • method3: compile (IllegalArgumentException is a runtime exception, so it's not subject to the handle-or-declare rule)


  • The incorrect one: method4 does not compile. Why? All checked exception adhere to the handle-or-declare rule, so that's definitely not the problem. But if you look closely to both catch handlers, you'll see both variables have the same name e. So you'll have a duplicate name and that's the reason why it doesn't compile. If you choose different names for both parameters (like in method1 and method2) the code will compile successfully. Like I said it was a (very) tricky question But you'll almost certain to encounter a few similar questions like this on the actual exam (e.g. the question contains a complex for loop, so it seems the question will test you knowledge of loops; but in fact a variable is used outside its scope, so the question is testing your knowledge about variable scope)

    Hope it helps!
    Kind regards,
    Roel
     
    John Lerry
    Ranch Hand
    Posts: 145
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for the explanation, you are always precise and detailed.

    I have another question. I have noticed that there are, in the exam, questions on some exceptions throw by the JVM and some throw by the developer. For this type of questions I have to try to remember the table at the end of this chapter 6 but I would have some doubts about the other exceptions.
    To determine which exceptions are unchecked should I try to learn the most exceptions subclasses of RuntimeException? In few words, is it a work of memory?
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    John Lerry wrote:I have another question. I have noticed that there are, in the exam, questions on some exceptions throw by the JVM and some throw by the developer. For this type of questions I have to try to remember the table at the end of this chapter 6 but I would have some doubts about the other exceptions.
    To determine which exceptions are unchecked should I try to learn the most exceptions subclasses of RuntimeException? In few words, is it a work of memory?

    Have a look at this thread. I think it contains very useful information and might clear your doubts. If you still have any questions/doubts after reading this thread, don't hesitate to ask them in another reply.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic