package Demo3;
public class RuntimeExceptionDemo1 {
public static void stringToUpper(String str) {
if (str != null) {
System.out.println(str.toUpperCase());
}
}
public static void main(String[] args) {
// NullPointException
// 객체를 참조하지 않는 참조변수(null값을 가지는 참조변수)로
// 객체의 속성이나 기능을 사용할 때 발생되는 예외다.
String text = null;
//System.out.println(text.length());
RuntimeExceptionDemo1.stringToUpper(text);
}
}
package Demo3;
public class RuntimeExceptionDemo2 {
public static void main(String[] args) {
// ArrayIndexOutOfBoundException : 배열의 인덱스 범위를 벗어났을 때 발생
// StringIndexOutOfBoundsException : 문자열의 인덱스범위를 벗어났을 때 발생
String[] names = {"김유신", "강감찬", "이순신"};
// System.out.println(names[3]);
String text = "abc";
String subText = text.substring(2, 4);
}
}
package Demo3;
public class RuntimeExceptionDemo3 {
public static void main(String[] args) {
// NumberformatException : 문자열을 숫자로 바꿀 때 숫자로 변환할 수 없는 문자열이 포함되어 있으면 발생
// int Integer.parseInt(String numberText)
// double double.parseDouble(String numberText)
String text1 = "1234";
int number1 = Integer.parseInt(text1);
System.out.println(number1 * 3);
String text2 = "1234a";
String text3 = " 1234";
int number2 = Integer.parseInt(text2);
System.out.println(number2 * 3);
}
}
package Demo3;
import java.util.Scanner;
public class RuntimeExceptionDemo4 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("몸무게 입력> ");
String value1 = scanner.next();
System.out.println("키 입력> ");
String value2 = scanner.next();
double weight = Double.parseDouble(value1);
double height = Double.parseDouble(value2)/100;
double bmi = weight / (height + height);
System.out.println("bmi 지수 : " + bmi);
if(bmi > 35) {
System.out.println("고도 비만");
} else if (bmi > 30) {
System.out.println("중등도 비만");
} else if (bmi > 25) {
System.out.println("경도 비만");
} else if (bmi > 23) {
System.out.println("과체중");
} else if (bmi > 18.5) {
System.out.println("저체중");
}
scanner.close();
}
}
package Demo3;
public class RuntimeExceptionDemo5 {
public static void main(String[] args) {
String text = "dddd";
int value = StringUtils.stringToInt(text);
System.out.println("숫자값 : " + value);
}
}
package Demo3;
public class RuntimeExceptionDemo6 {
public static void main(String[] args) {
// arithmaic Excpetion 0으로 어떤 수를 나눌 때 }
System.out.println(3/0);
}
}
package Demo3;
public class StringUtils {
public static int stringToInt(String text) {
int value;
try {
value = Integer.parseInt(text);
} catch (NumberFormatException e) {
value = 0;
}
return value;
}
}