방금 자바를 배우기 시작했습니다. 이건 내 예외 클래스는던지기 예외가 throw되지 않습니다
public class Car {
protected String type;
protected String[] colors;
protected boolean isAvaiable;
public Car(String type, Collection<String> colors, boolean isAvaiable) throws NoColorException {
if (colors == null || colors.isEmpty()) {
throw new NoColorException("No colours!");
} else {
this.type = type;
this.colors = (String[]) colors.toArray();
this.isAvaiable = isAvaiable;
}
}
public static void main(String[] args) {
try {
Car n = new Car("asd", new ArrayList(), true);
} catch (NoColorException ex) {
}
}
}
이다 : 나는 예외 처리가 그래서 난이 작은 프로그램을 만들어 작동 방식을 배우려고 노력하고있어
public class NoColorException extends Exception {
public NoColorException(String string) {
super(string);
}
}
내가 만들려고 할 때 예외를 발생합니다 위의 코드 객체가 아니라 대신 실행됩니다.
왜 이런 일이 발생합니까?
도움을 주시면 대단히 감사하겠습니다.
catch (NoColorException ex) {
}
@ l4mpi 왜 컴파일되지 않을까요? main() 메서드이므로 컴파일러는 이벤트에 대해서만 경고합니다 –
@ l4mpi 그리고 아무 IDE도 그것에 대해 불평하지 않습니다 –
죄송합니다. "throws"선언을 놓쳤습니다. 하지만 여전히 "붉은 색의 텍스트를 보려면"부분은 완전히 말도 안되며 실행 환경에 따라 100 % 달라집니다. – l4mpi