This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assertion Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assertion Question" Watch "Assertion Question" New topic
Author

Assertion Question

Mike Cutter
Ranch Hand

Joined: Jun 09, 2002
Posts: 49
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

Joined: May 05, 2000
Posts: 13974
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 ]

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
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

Joined: Sep 22, 2001
Posts: 2578


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

Joined: Aug 05, 2002
Posts: 787
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

Joined: May 05, 2000
Posts: 13974
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Assertion Question
 
Similar Threads
assertion doubt
Assertions related
Javacertificate.com question on assertions
assertions
Packages/Subpackages