2024-11-04

str1 문자열 길이 >>> 4
java.lang.NullPointerException: Cannot invoke "String.length()" because "str2" is null
at basic.Exception_08.exception1(Exception_08.java:15)
at basic.Exception_08.main(Exception_08.java:47)
java.lang.ArithmeticException: / by zero
at basic.Exception_08.exception2(Exception_08.java:35)
at basic.Exception_08.main(Exception_08.java:49)
package basic;
public class Exception_08 {
void exception1 () {
String str1 = "java";
String str2 = null;
try {
System.out.println("str1 문자열 길이 >>> " + str1.length());
System.out.println("str2 문자열 길이 >>> " + str2.length());
}catch(Exception e) {
e.printStackTrace();
/*
* printStackTrace()
* - 에러 메세지의 발생 근원을 찾아서
* 단계별로 에러 메세지를 출력해 주는 메서드.
*/
}
}
void exception2() {
int num1 = 14, num2 = 0, result = 0;
try {
result = num1 / num2;
}catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Exception_08 exception = new Exception_08();
exception.exception1();
exception.exception2();
}
}
'Java > 기초 내용 정리' 카테고리의 다른 글
| Java(Exception)_Exam_01 (0) | 2024.11.04 |
|---|---|
| Java(Exception)_09 (3) | 2024.11.04 |
| Java(Exception)_07 (0) | 2024.11.04 |
| Java(Exception)_06 (0) | 2024.11.04 |
| Java(Exception)_05 (0) | 2024.11.04 |