ASSERTIONS.pptx

12
UNIT IV ASSERTIONS & LOGGING

Transcript of ASSERTIONS.pptx

Page 1: ASSERTIONS.pptx

7/27/2019 ASSERTIONS.pptx

http://slidepdf.com/reader/full/assertionspptx 1/12

UNIT IV

ASSERTIONS & LOGGING

Page 2: ASSERTIONS.pptx

7/27/2019 ASSERTIONS.pptx

http://slidepdf.com/reader/full/assertionspptx 2/12

ASSERTIONS

• An assertion is a statement in the JavaTM programming language that enables you totest your assumptions about your program

• Each assertion contains a boolean expressionthat you believe will be true when theassertion execute

writing assertions while programming is oneof the quickest and most effective ways todetect and correct bugs

Page 3: ASSERTIONS.pptx

7/27/2019 ASSERTIONS.pptx

http://slidepdf.com/reader/full/assertionspptx 3/12

• The assertion statement has two forms. The

first, simpler form is:

assert Expression1 ;

• The second form of the assertion statement is:

assert Expression1 : Expression2 ;

where:

• Expression1 is a boolean expression.

• Expression2

is an expression that has a value.

(It cannot be an invocation of a method that is

declared void.)

Page 4: ASSERTIONS.pptx

7/27/2019 ASSERTIONS.pptx

http://slidepdf.com/reader/full/assertionspptx 4/12

• The purpose of the detail message is tocapture and communicate the details of the

assertion failure• The message should allow you to diagnose

and ultimately fix the error that led theassertion to fail

• assertion failures are generally labeled in thestack trace with the file and line number fromwhich they were thrown

• assertions can be enabled or disabled whenthe program is started, and are disabled bydefault

Page 5: ASSERTIONS.pptx

7/27/2019 ASSERTIONS.pptx

http://slidepdf.com/reader/full/assertionspptx 5/12

Putting Assertions Into Code

• There are also a few situations where you

should not use them – Do not use assertions for argument checking in

public methods.

 –Do not use assertions to do any work that yourapplication requires for correct operation.

Page 6: ASSERTIONS.pptx

7/27/2019 ASSERTIONS.pptx

http://slidepdf.com/reader/full/assertionspptx 6/12

• There are many situations where it is good to

use assertion

 – Internal Invariants

 – Control-Flow Invariants

 – Preconditions, Postconditions, and Class Invariants

Page 7: ASSERTIONS.pptx

7/27/2019 ASSERTIONS.pptx

http://slidepdf.com/reader/full/assertionspptx 7/12

if (i % 3 == 0) {

...

} else if (i % 3 == 1) {

...

} else { // We know (i % 3 == 2) ...

}

rewrite the previous if-statement like this:

if (i % 3 == 0) {

...

} else if (i % 3 == 1) {

...

} else {

assert i % 3 == 2 : i; 

...

}

Page 8: ASSERTIONS.pptx

7/27/2019 ASSERTIONS.pptx

http://slidepdf.com/reader/full/assertionspptx 8/12

• Control-Flow Invariantsvoid foo() {

for (...)

{

if (...)return;

}

// Execution should never reach this point!!! 

• }

• Replace the final comment so that the code nowreads:

void foo()

{ for (...)

{if (...)

return; }

assert false;

// Execution should never reach this point!

}

Page 9: ASSERTIONS.pptx

7/27/2019 ASSERTIONS.pptx

http://slidepdf.com/reader/full/assertionspptx 9/12

• Preconditions, Postconditions, and ClassInvariants

• Preconditions— what must be true when amethod is invoked. – Lock-Status Preconditions— preconditions concerning

whether or not a given lock is held.

• Postconditions— what must be true after amethod completes successfully.

• Class invariants—

what must be true about eachinstance of a class.

•  

Page 10: ASSERTIONS.pptx

7/27/2019 ASSERTIONS.pptx

http://slidepdf.com/reader/full/assertionspptx 10/12

Logging

• Logging is the process of writing log messagesduring the execution of a program to a centralplace

• This logging allows you to report and persistserror and warning messages as well as infomessages (e.g. runtime statistics) so that themessages can later be retrieved and analyzed.

• Individual classes can use this logger to writemessages to the configured log files

Page 11: ASSERTIONS.pptx

7/27/2019 ASSERTIONS.pptx

http://slidepdf.com/reader/full/assertionspptx 11/12

• Create a logger

To create a logger we can use the following code

import java.util.logging.Logger; // assumes thecurrent class is called logger private final static

Logger LOGGER =

Logger.getLogger(MyClass.class.getName());

Page 12: ASSERTIONS.pptx

7/27/2019 ASSERTIONS.pptx

http://slidepdf.com/reader/full/assertionspptx 12/12

Level• The following lists the Log Levels in

descending order:• SEVERE (highest)

• WARNING

• INFO

• CONFIG

• FINE

• FINER

• FINEST