Ich weiß, dass wir Fehler in Java mit try catch nicht behandeln sollten, aber ich habe es zum Lernen ausprobiert und festgestellt, dass wir Fehler in ähnlicher Weise umgehen können. Exception mit try catch block. Selbst Code nach catch-Block wird ebenfalls ausgeführt. Ich habe einen StackOverFlowError generiert, kann aber trotzdem lokale Variablen initialisieren. Kann jemand bitte es erklären?Warum wird die Fehlerbehandlung nicht empfohlen?
import java.util.LinkedList;
import java.util.List;
public class ExceptionDemo {
public static void main(String[] args) {
try{
method1();
}
catch(Error e){
System.out.println(e.getStackTrace());
System.out.println("Hello world");
int a =10;
System.out.println(a);
method();
}
System.out.println("Hello world2");
}
public static void method1(){
method1();
}
public static void method(){
List l1 = new LinkedList();
l1.add("A");
int[] aa = new int[10000];
aa[0]=25;
System.out.println(aa[0]);
int b =10;
int c = 20;
System.out.println(b +""+c);
}
}
Ausgang dieser Klasse ist -
[Ljava.lang.StackTraceElement;@1db9742
Hello world
10
25
1020
Hello world2
was fragen Sie? –
Willkommen bei Stack Overflow! Bitte nimm die [Tour], schau dich um und lies dir die [Hilfe] durch, insbesondere [* Wie stelle ich eine gute Frage? *] (/ Help/how-to-ask) –
Aber trotzdem kann ich initialisieren Objekt und lokale Variablen, wie es passiert? –