Code: Select all
while(true);
Test 1: < /strong> < /h2>
Code: Select all
public class While {
public static void main(String[] args) {
System.out.println("start");
while (true);
System.out.println("end");
}
}
< /code>
Der Compiler wirft mir den Fehler aus: < /em> < /strong> < /p>
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Unreachable code
at While.main(While.java:6)
< /code>
Ich wusste nicht, dass dieser Fehler existiert. Aber ich habe bekommen, warum es geworfen wird. Natürlich war Zeile 6 nicht erreichbar [/b] und verursachte ein Zusammenstellungsproblem. Dann habe ich dies getestet: < /p>
[b] Test 2: < /strong> < /h2>
public class While {
public static void main(String[] args) {
System.out.println("start");
a();
b();
}
static void a() {
while(true);
}
static void b() {
System.out.println("end");
}
}
< /code>
Aus irgendeinem Grund Das Programm lief normal < /strong> (die Konsole "Start" und dann eingefroren). Der Compiler konnte innerhalb von nicht eincheckenvoid a()
Test 3: < /strong> < /h2>
Code: Select all
public class While {
public static void main(String[] args) {
System.out.println("start");
a();
System.out.println("end");
}
static void a() {
while(true);
}
}
< /code>
Gleiches Ergebnis wie Test 2. Strong> Frage [/b]. , wenn der Code in den Klammern eine Variable ist, würde der Compiler die Ausnahme nicht auswerfen . Das macht Sinn, aber ich denke nicht, dass dasselbe für [b] giltvoids
Code: Select all
void b()
Code: Select all
System.out.println("end");
#include
using namespace std;
int main()
{
cout