기타 보관함/개발자정보
Java Exceptions
오아름 샘
2022. 6. 4. 09:38
반응형
자바 예외상황 처리(Exceptions)
자바 코드를 실행할 때 프로그래머가 만든 코딩 오류, 잘못된 입력으로 인한 오류, 또는 예측할 수 없는 다른 오류와 같은 다른 오류가 발생할 수 있다.
오류가 발생하면 Java는 일반적으로 중지되고 오류 메시지를 생성합니다. 이에 대한 전문 용어는 다음과 같습니다: Java는 예외를 발생시킵니다(오류 발생).
자바 구문
try 문을 사용하면 실행 중에 오류를 테스트할 코드 블록을 정의할 수 있습니다.
catch 문을 사용하면 try 블록에서 오류가 발생할 경우 실행할 코드 블록을 정의할 수 있습니다.
try와 catch 키워드는 쌍으로 제공됩니다.
try {
// Block of code to try
}
catch(Exception e) {
// Block of code to handle errors
}
public class Main {
public static void main(String[ ] args) {
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]); // error!
}
}
public class Main {
public static void main(String[] args) {
try {
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]);
} catch (Exception e) {
System.out.println("Something went wrong.");
} finally {
System.out.println("The 'try catch' is finished.");
}
}
}
반응형