프로그램 실행 중 예기치 못한 사건을 예외라고 한다.
예외 상황을 미리 예측하고 처리할 수 있는데, 이렇게 하는 것을 예외 처리라고 한다.
public class ExceptionExam {
public static void main(String[] args) {
int i = 10;
int j = 5;
int k = i / j;
System.out.println(k);
System.out.println(main 종료!!);
}
}
try
라는 블록으로 감싼 후,catch
라는 블록에서 처리한다.finally
라는 블록을 가질 수 있다. public class ExceptionExam {
public static void main(String[] args) {
int i = 10;
int j = 0;
try{
int k = i / j;
System.out.println(k);
}catch(ArithmeticException e){
System.out.println("0으로 나눌 수 없습니다. : " + e.toString()); //예외변수명.toString()
}finally { //: 예외의 정보를 알려주는 메소드
System.out.println("오류가 발생하든 안하든 무조건 실행되는 블록입니다.");
}
}
}
실행 결과
0으로 나눌 수 없습니다. : java.lang.ArithmeticException: / by zero
오류가 발생하든 안하든 무조건 실행되는 블록입니다.
Exception 처리하지 않았을때는 프로그램이 강제 종료되었는데 catch라는 블록으로 Exception을 처리하니 강제종료되지 않고 잘 실행되는 것을 알 수 있다.
try블록에서 여러종류의 Exception이 발생한다면 catch라는 블록을 여러개 둘 수 있다.
Exception클래스들은 모두 Exception클래스를 상속받으므로, 예외클래스에 Exception을 두게 되면 어떤 오류가 발생하든지 간에 하나의 catch블록에서 모든 오류를 처리할 수 있다.
throws
는 예외가 발생했을때 예외를 호출한 쪽에서 처리하도록 던져준다.
Exception처리를 무시하는 키워드
public class ExceptionExam2 {
public static void main(String[] args) {
int i = 10;
int j = 0;
int k = divide(i, j);
System.out.println(k);
}
public static int divide(int i, int j){
int k = i / j;
return k;
}
}
public class ExceptionExam2 {
public static void main(String[] args) {
int i = 10;
int j = 0;
int k = divide(i, j);
System.out.println(k);
}
public static int divide(int i, int j) throws ArithmeticException{
int k = i / j;
return k;
}
}
package javaStudy;
public class ExceptionExam2 {
public static void main(String[] args) {
int i = 10;
int j = 0;
try{
int k = divide(i, j);
System.out.println(k);
} catch(ArithmeticException e){
System.out.println("0으로 나눌수 없습니다.");
}
}
public static int divide(int i, int j) throws ArithmeticException{
int k = i / j;
return k;
}
}
강제로 오류를 발생시키는
throw
public class ExceptionExam3 {
public static void main(String[] args) {
int i = 10;
int j = 0;
int k = divide(i, j);
System.out.println(k);
}
public static int divide(int i, int j){
int k = i / j;
return k;
}
}
public class ExceptionExam3 {
public static void main(String[] args) {
int i = 10;
int j = 0;
int k = divide(i, j);
System.out.println(k);
}
public static int divide(int i, int j){
if(j == 0){
System.out.println("2번째 매개변수는 0이면 안됩니다.");
return 0;
}
int k = i / j;
return k;
}
}
public class ExceptionExam3 {
public static void main(String[] args) {
int i = 10;
int j = 0;
int k = divide(i, j);
System.out.println(k);
}
public static int divide(int i, int j) throws IllegalArgumentException{
if(j == 0){
throw new IllegalArgumentException("0으로 나눌 수 없어요."); //예외 발생시키기
}
int k = i / j;
return k;
}
}
public class ExceptionExam3 {
public static void main(String[] args) {
int i = 10;
int j = 0;
try{
int k = divide(i, j);
System.out.println(k);
}catch(IllegalArgumentException e){
System.out.println("0으로 나누면 안됩니다.");
}
}
public static int divide(int i, int j) throws IllegalArgumentException{
if(j == 0){
throw new IllegalArgumentException("0으로 나눌 수 없어요.");
}
int k = i / j;
return k;
}
}