• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Error Throwing Question

 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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 ]
 
Sunglasses. AKA Coolness prosthetic. This tiny ad doesn't need shades:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic