Ich schrieb diesen Code mit try/catch machen, und ich will es diese zur Ausgabe:try/catch-Methode einen Parameter zu erhalten, die eine bestimmte Ausgabe von einer Methode in Java
A null pointer exception occurred
In the finally block
The length of the string is 1
A number format exception occurred
In the finally block
A null pointer exception occurred
Some type of exception occurred
In the finally block
The length of the string is 1
The value is 5
aber für einige Grund, wenn ich rufe Ich versuche, es von der Hauptsache zu bekommen, bekomme ich einen Fehler, so ist meine Frage, wie kann ich die Methode richtig von der Haupt anrufen, um die Ausgabe oben zu bekommen?
dies ist die Methode, die ich schrieb:
public static void problem2(String s) {
try {
int stringLen = s.length();
System.out.println("The length of the string is " + stringLen);
int i = Integer.parseInt(s);
System.out.println("The value is " + i);
} catch (NumberFormatException e1) {
System.out.println("A number format exception occurred");
} catch (NullPointerException e2) {
System.out.println("A null pointer exception occurred");
} catch (Exception e3) {
System.out.println("Some type of exception occurred");
} finally {
System.out.println("In the finally block");
}
}
Was ist der Fehler, den Sie bekommen? – KevinO
Ich habe es gerade getestet und es funktioniert, so wie Kevin sagt, was ist der Fehler? –