도달할 수 없는 코드 오류는 무한 루프, 도달할 수 없는 코드 라인 앞의 return 문 등 다양한 이유로 인해 코드를 컴파일할 수 없을 때 발생합니다.
예를 들어 보겠습니다 -
예
public class Demo{
public static void main(String args[]){
int val = 5;
for (;;){
if (val == 5){
break;
System.out.println("If the condition is not true, this line would be printed. ");
}
}
}
} 출력
/Demo.java:11: error: unreachable statement
System.out.println("If the condition is not true, this line would be printed. ");
^
1 error Demo라는 클래스에는 메인 함수가 포함되어 있고 값이 정의되어 있으며 이 값을 확인하고 빈 'for' 루프를 실행합니다. 값을 찾으면 컨트롤이 루프에서 벗어나지 않으면 메시지를 인쇄합니다. 무한 루프이므로 도달할 수 없는 명령문 오류가 발생합니다.