Oct 30

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.

Tagged with:
Oct 28

Here I already explained the difference between ExceptionInInitializerError and NumberFormatException therefore today only one example of NumberFormatException:

public class Angestellter {

public int H = new Integer(“Test”);

public static void main(String args[]) {

Angestellter a = new Angestellter ();

System.out.println(a.H);

}

}

Tagged with:
Oct 26

IllegalStateException will be thrown by a method when the object is in a state which does not allow furter calls, eg:

Scanner scanner = new Scanner (System.in)

while (scanner.hasNext()){

scanner.close(); // close of scanner

}

After close of scanner can method scanner.hasNext() not executed anymore; an IllegalStateExcetion will be thrown

Tagged with:
Oct 24

An IllegalArgumentException will be thrown in case an invalid parameter value is passed. Since this a RuntimeException try/catch/finally-block is not neccessary. Mostly, this method is inserted by programmers, eg:

public class Angestellter {

int PersNr;

void setPersNr (int PersNr){

if (PersNr <=0) //PersNr < 0 is not logical and should be catched

throw new IllegalArgumentException();

else

this.PersNr=PersNr;

}

public static void main(String args[]) {

Angestellter a = new Angestellter();
a.setPersNr(0);

}

}

so in our case an invalid input of negative personal number is checked and if so an IllegalArgumentException is thrown.

Tagged with:
Oct 22

This error is in general an sign for missing java-archives in classpaths. Possible reasons could be

  • class is not set to public
  • classpath – environment variable need to contain the folder of the class, respectively the package
  • in case the class is included in a package; it need to be called over the package name, eg java package_name.myClass
  • invoking of the file need to be done without ending .*class

In the following months I will go deeper in classpaths and compilers, but 1st of all we still have some time to spent on exceptions and errors :)

Tagged with:
Oct 20

In case stack is full, StackOverflow error is thrown:

class Angestellter {

static void overflow () {

overflow();

}

public static void main(String args[]) {

overflow();

}

}

in our example a self calling method is defined which is exhausting the stack.

Tagged with:
Oct 18

ExceptionInInitializerError will be thrown in case of failure in static initalising :

class Angestellter {

public static int H = new Integer(“Test”); // “Test” can not be boxed into Integer

public static void main(String args[]) {

System.out.println(H);

}

}

Output could be similar to:

java.lang.ExceptionInInitializerError
Caused by: java.lang.NumberFormatException: For input string: “start”
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.<init>(Unknown Source)
at Angestellter.<clinit>(Angestellter.java:18)
Exception in thread “main”

Attention: In case of non-static variables a NumberFormatException will be thrown

Tagged with:
Oct 16

A NullPointerException will be thrown in case the Compiler does not have a value with whom he can work.

Eine NullPointer Exception wird ausgegeben, wenn der Compiler keinen Wert hat, mit dem er arbeiten kann:

class Angestellter {

public int PersNr;

public static void main(String args[]) {

Angestellter a = null; // a points to null, PersNr is not intialized
System.out.println(a.PersNr); //  NullPointerException


}

}

An additional – quite often appearing – example:

String s = null;
System.out.println(s.length());
// Same logic is applied here; since s is not initialized there is nothing to access for method length

Tagged with:
Oct 14

Let’s go for an example first; under the precondition that classes Dalmatian and Terrier are derivated independently from class Dog lead

Dalmatian a = new Dalmatian ();
Terrier b = (
Terrier) a ;

directly to CompilerError, but

Dog c = new Dalmatian(); //ok

Terrier d = (Terrier) c; //ClassCastException

as well as

Dalmatian e = (Dalmatian) new Dog ();// Dog can get the properties of his subclasses, but its subclasses can not get the properties of its base class

will be recognized as problem only at runtime and ClassCastException is thrown.  Background is that Dalmatian and Terrier are not derivated from each other (only in each case from class dog). Therefore a Dalmatian can not be a Terrier and the other way around.

Tagged with:
Oct 12

ArrayIndexOutOfBoundsException occurs in case of accesss to array-index which is not existent:

int a [ ] = {10,20};
a[2]=30;

This is not resulting in CompilerError,  but an exception is thrown at runtime.

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