• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assertion Question

 
Mike Cutter
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was perusing a Java 1.4 certification book in the bookstore. I saw this assertion question, but I never looked up the answer and explanations. I might know two of the answers just based on educated guesses. If you all could give me a better understanding, it would be greatly appreciated.
All I know about assertions is that it is new in Java 1.4 and that you have to use "javac -source 1.4" and "java -ea".
Which three statements are true? (Choose three)
A. Assertion checking can be selectively enabled or disabled on a per-package basis, but not a per-class basis.
B. Assertion checking is typically enabled when a program is deployed.
C. Assertion checking can be selectively enabled or disabled on both a per-package basis and a per-class basis.
D. It is never appropriate to write code to handle failure of an assert statement.
E. Assertion checking is typically enabled during program development and testing.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C, D, and E.
Check the September issue of the JavaRanch newsletter for two exceptionally well written articles on assertions!
http://www.javaranch.com/newsletter/Sept2002/newslettersept2002.jsp
[ September 10, 2002: Message edited by: Thomas Paul ]
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul:
I read both: your artical and sun's resource on assertion. I have following question/comments:
1. assert seems like programmer defined "exception".. In other words, java comes with pre-defined exceptions that include compile time and run time exceptions. Now programmer can create his/her own "exception" and terminate program as a result.
2. In FoxPro, I had a choice to continue execution after an assertion situation occurs and message is displaced. I could continue execution or go in debug mode or terminate execution. I guess Java does not provide such facility. Right?
3. Argument to assert statement (i.e. boolean expresion): can it be a method call with a return type of boolean?
Thanks
Barkat
 
Manish Hatwalne
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1. assert seems like programmer defined "exception".. In other words, java comes with pre-defined exceptions that include compile time and run time exceptions. Now programmer can create his/her own "exception" and terminate program as a result.

assert statements are *NOT* like programmer defined exceptions, to do so you can directly extend the required exception. Exceptions always get evaluated whereas assertion gets evaluated ONLY IF it is enabled.

2. In FoxPro, I had a choice to continue execution after an assertion situation occurs and message is displaced. I could continue execution or go in debug mode or terminate execution. I guess Java does not provide such facility. Right?

I do not know abt FoxPro, but in Java you have a choice of catching an AssertionError, and acting accordingly but since it is an error, it is discouraged.

3. Argument to assert statement (i.e. boolean expresion): can it be a method call with a return type of boolean?

Yes, it can be a method call with a return type of boolean.
HTH,
- Manish
[ September 11, 2002: Message edited by: Manish Hatwalne ]
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Manish:
Thanks for your replay...

Originally posted by Manish:
assert statements are *NOT* like programmer defined exceptions, to do so you can directly extend the required exception. Exceptions always get evaluated whereas assertion gets evaluated ONLY IF it is enabled.

By extending the existing exceptions, Would you be able to coin any concievable exception? I thought asserts give you much more flexibility....
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Barkat Mardhani:
By extending the existing exceptions, Would you be able to coin any concievable exception? I thought asserts give you much more flexibility....
Asserts are not designed for exception handling. They are designed to assist the developer during development.
You can create any exception you like simply be creating a new class that extends Exception.

And that is all you need to do.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic