• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Error Throwing Question

 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

I'm really confused about what should I consider as good or bad practice when explicitly throwing an Error for the exam.

The following two questions were extracted from K&B Master Exam #2 and from Complete Java 2 Certification Study Guide 5th Edition, respectively.

It seems the first one doesn't consider throwing explicitly an Error as bad practice whereas the second one considers throwing explicitly an Error as a bad practice.

So now I'm really confused about what should I consider as true.

Please I'm really interested about your comments.

Please check it out :

-------------------------------------------
From K&B Master Exam #2.

Which are true ? (Choose all that apply)

a) It's not good practice to place assertions where you think execution should never reach.
b) It's sometimes appopriated to call getters and setters from assertions.
c) Use assertions to verify arguments of private methods.
d) Assertions can be disabled for a particular class.
e) Never throw an AssertionError explicitly.

C and D are correct but I thought the correct answers were C, D and E.

-------------------------------------------
Complete Java 2 Certification Study Guide 5th Edition

When is it appropriate to write code that constructs and throws an error?

a) When a public method�s preconditions are violated
b) When a public method�s postconditions are violated
c) When a nonpublic method�s preconditions are violated
d) When a nonpublic method�s postconditions are violated
e) Never

The correct answers is E. According to author's explanation the reason is : "It is never appropriate for application programmers to construct and throw errors."

Once Error is the super class of AssertionError. I think Both questions are totally related to each other.
[ April 14, 2006: Message edited by: Edisandro Bessa ]
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, it is true, most of the books recommend you not to throw Error exceptions explicitly.

Nevertheless, some authors recommend throwing AssertionError in Control Flow Assertions.

The following examples are partly taken from The Java Programming Language, Fourth Edition (by Ken Arnold, James Gosling and David Holmes)

I made some small modifications in order to make it clearer.

In this code we expect that a value is always found in the array. Hence, we can conclude that the assertion would never be evaluated. Well that is the point of assertions, assert conditions that should never happen.

Now, although we expect the assertion code never be evaluated, we're still obliged to return a "bogus" value, because the code is actually reachable since the point of view of the compiler.



Now, if we would like to avoid this "bogus return", knowing that the code should never reach that point, than we can explicitly throw the AssertionError, like this:



However in this case, the exception would happen whether exceptions are enabled or not whenever the value is not found. In this case there is no bogus return. If you turn off assertion error your program would still crash in this line if the value is not found.

Of course I could have avoided using this implementation by means of using a break in the loop and assertion evaluation before returning the found value, like this:



I guess this is what your book might be referring to.

[ April 14, 2006: Message edited by: Edwin Dalorzo ]
[ April 14, 2006: Message edited by: Edwin Dalorzo ]
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Huuumm. Thanks Edwin.

All of your explanations gave me a better idea about what the questions were supposed to be referring to.

I got the point. Thanks.
[ April 14, 2006: Message edited by: Edisandro Bessa ]
 
Amateurs built google. Professionals built the titanic. We can't find the guy that built this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic