2024-11-04

프로그램 시작
str1 문자열의 길이 >>> 5
null 값을 갖는 오류 발생
예외 정보 >>> java.lang.NullPointerException: Cannot invoke "String.length()" because "str2" is null
프로그램 종료
package basic;
// Exception_02 클래스에서 발생한 예외를
// 예외 처리 코드를 이용하여 처리해 보세요.
public class Exception_05 {
public static void main(String[] args) {
System.out.println("프로그램 시작");
String str1 = "Korea";
String str2 = null; // 값이 없는 상태를 null 이하고 함.
try {
// length() : 현재 문자열의 길이를 정수값으로 반환해 주는 메서드.
System.out.println("str1 문자열의 길이 >>> " + str1.length());
System.out.println("str2 문자열의 길이 >>> " + str2.length());
}catch(Exception e) {
System.out.println("null 값을 갖는 오류 발생");
System.out.println("예외 정보 >>> " + e);
}
System.out.println("프로그램 종료");
}
}'Java > 기초 내용 정리' 카테고리의 다른 글
| Java(Exception)_07 (0) | 2024.11.04 |
|---|---|
| Java(Exception)_06 (0) | 2024.11.04 |
| Java(Exception)_04 (0) | 2024.11.04 |
| Java(Exception)_03 (0) | 2024.11.04 |
| Java(Exception)_02 (0) | 2024.11.04 |