Nov 16

On which point(s) would an exception occur?

String PersNr = null;

if ( PersNr != null && PersNr.length() > 0)
System.out.println(“PersNr != null & PersNr.length() > 0″);

if ( PersNr != null | PersNr.length() > 0)
System.out.println(“PersNr != null | PersNr.length() > 0″);

if ( PersNr != null || PersNr.length() > 0)
System.out.println(“PersNr != null & PersNr.length() > 0″);

if ( PersNr != null & PersNr.length() > 0)
System.out.println(“PersNr != null & PersNr.length() > 0″);

Antwort/ answer
Tagged with:
Nov 14

Is this code compiling faultless?

class NewException extends IOException {

void test() throws NeueException {

throw NeueException();

}

}



Antwort/ answer

Tagged with:
Nov 12

Which exceptions are thrown by JVM and which are related to programmer?

  1. AssertionError
  2. ClassCastException
  3. NullPointerException
  4. IllegalStateException
  5. ExceptionInInitializerError
  6. StackOverflowError
  7. NoClassDefFoundError
  8. IllegalArgumentException
  9. ArrayIndexOutOfBoundsException
  10. NumberFormatException
Antwort/ answer
Tagged with:
Nov 10

What is the output of the code?

String str = null;
try {

if (str.length() == 0) { System.out.print(“1″); }

System.out.print(” 2″);

} catch (Exception e) {

System.out.print(” 3″);
System.exit(0);

} finally {

System.out.print(” 4″);

}
System.out.println(” 5″);


Antwort/ answer
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 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:
preload preload preload
http://www.wikio.de Blog Top Liste - by TopBlogs.de Blogverzeichnis - Blog Verzeichnis bloggerei.de Bloggeramt.de Software
Webbhotell Top Blogs