Oct 30

This post is also available in: German

Assertions Errors or shortly Assertions are code checks.

Let’s have a look on the following example:

Angestellter a = new Angestellter ();
Scanner s = new Scanner (System.in);
a.PersNr=s.nextInt();

s.close();

before Assertions, wrong values whould be checked in a similar way to this:

if (a.PersNr > 0)

System.out.println(“ok”);

else

System.out.println(“not ok”);

with help from Assertions this 4 lines can be compressed in one line:

assert (a.PersNr > 0);

Only in case assert-condition is not true, an AssertionError is thrown … and you know what is the best on it? You can enable and disable it with options -ea (on) respectively -da (off)!

For eclipse, it can be done eg in menu  File/Properties/Run-Debug-Settings/Edit/Reiter Arguments/ Field VM-Arguments:  -ea

The SCJP-DOS-relevant compiling I will explain later in more details.

  • Print
  • del.icio.us
  • Facebook
  • Twitter
  • Google Bookmarks
  • Add to favorites
  • RSS
  • Blogosphere
  • email
  • PDF
  • Wikio
  • Technorati
  • Digg
  • DZone

Leave a Reply

preload preload preload
http://www.wikio.de Blog Top Liste - by TopBlogs.de Blogverzeichnis - Blog Verzeichnis bloggerei.de Bloggeramt.de Software
Webbhotell Top Blogs