Beny's Study
ArithmeticException(산술예외) 본문
ArithmeticException
산술 예외
오류코드
public class RuntimeException {
public static void main(String[] args) {
int a = 10 / 0;
}
}
- 이 코드 실행시 오류발생하고 ArithmeticException 뜸
-why? 정수는 0으로 나눌수 없기 때문!
- 이때 예외처리문 try ~catch로 예외처리를 해준다
예외처리 코드
public class RuntimeException {
public static void main(String[] args) {
int a = 10 ;
int b = 0;
int result = a/b;
try{
system.out.println(a+"을" + b+"로 나눈값은" +result+"입니다.");
}catch(Exception e){
system.out.println("0으로 나눌수 없습니다.");
}finally{
system.out.println("예외처리가 끝났습니다.");
}
}
}
-0으로 나눌 경우 예외로 [0으로 나눌 수 없습니다]를 출력해준다.
"본 인터넷 사이트 내의 모든 이미지, 문구, 콘텐츠, 내용 등에 대한 저작권은 76beny에게 있습니다.
이를 무단으로 도용, 복사, 전재, 재배포, 2차 변형 등을 할 경우
민, 형사상 법적 조치 등 저작권법에 의거하여 처벌 받을 수 있습니다."
'[JAVA] > 11.예외처리' 카테고리의 다른 글
try~catch 문 _ 예외처리 문법 (0) | 2022.04.20 |
---|---|
java.lang.OutOfMemoryError: Java heap space(시스템에러_힙) (0) | 2022.04.20 |
OutOfMemoryError : Required array length too large(시스템에러_스텍) (0) | 2022.04.20 |
NullPointerException(null예외) (0) | 2022.04.20 |
NegativeArraySizeException(배열사이즈 음수오류) (0) | 2022.04.20 |