Hi Leute,
Ich habe mal ne Frage zu den try und catch Blöcken, dazu poste ich mal kurz zwei mini Programme, anhand derer ich meine
Frage dann stellen werde. Beide Programme sollen mit Start Argumenten ausgeführt werden, jedoch funktioniert das 2. Programm nicht.
Programm 1:
public class Frage{
public static void main (String [] args) {
int a = Integer.parseInt(args[0]);
System.out.println(a);
}
}
------------------------------------------------------------------
Programm 2 :
public class Frage2 {
public static void main (String []args) {
try {
int a = Integer.parseInt(args[0]);
}
catch(NumberFormatException b) {
System.out.println("Aufrufparameter ist nicht zulässig");
}
System.out.println(a);
}
}
--------------------------------------------------------------------
Als Fehlermeldung erhalte ich:
Cannot find variable a ....
Das kann ich jedoch überhaupt nicht verstehen, da ich die variable a ja deklariert und durch parse-Methode ihr
auch einen Wert zugewiesen habe. Wieso funktioniert es bei Programm 1 einwandfrei und bei Programm 2 nicht??
Mit dem try und catch Block will ich doch lediglich eine mögliche NumberFormatException abfangen.
Ich hoffe, dass Ihr Profis mir helfen könnt.
Ich habe mal ne Frage zu den try und catch Blöcken, dazu poste ich mal kurz zwei mini Programme, anhand derer ich meine
Frage dann stellen werde. Beide Programme sollen mit Start Argumenten ausgeführt werden, jedoch funktioniert das 2. Programm nicht.
Programm 1:
public class Frage{
public static void main (String [] args) {
int a = Integer.parseInt(args[0]);
System.out.println(a);
}
}
------------------------------------------------------------------
Programm 2 :
public class Frage2 {
public static void main (String []args) {
try {
int a = Integer.parseInt(args[0]);
}
catch(NumberFormatException b) {
System.out.println("Aufrufparameter ist nicht zulässig");
}
System.out.println(a);
}
}
--------------------------------------------------------------------
Als Fehlermeldung erhalte ich:
Cannot find variable a ....
Das kann ich jedoch überhaupt nicht verstehen, da ich die variable a ja deklariert und durch parse-Methode ihr
auch einen Wert zugewiesen habe. Wieso funktioniert es bei Programm 1 einwandfrei und bei Programm 2 nicht??
Mit dem try und catch Block will ich doch lediglich eine mögliche NumberFormatException abfangen.
Ich hoffe, dass Ihr Profis mir helfen könnt.