5-2주차 시작
Exception
●Exception
public class ExceptionExam {
public static void main(String[] args) {
int i = 10;
int j = 2;
int k = i / j;
System.out.println(k); // 출력 2
}
}
public class ExceptionExam {
public static void main(String[] args) {
int i = 10;
int j = 0; // Excption 발생
int k = i / j; //실행이 되지않고 프로그램 종료
System.out.println(k); // 출력 2
}
}

●예외처리
●예외처리하는 문법

출처 : https://school.programmers.co.kr/learn/courses/5/lessons/244
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() //예외의 정보를 알려주는 메소드
}
System.out.println("main 종료!!");
}
}
출력

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("오류가 발생했든 발생하지 않았든 무조건실행");
}
System.out.println("main 종료!!");
}
}
출력

Throws
●Throws
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{
//오류를 처리하지않고 divide를 호출한 쪽에게 오류를 처리하라고 떠넘길수가 있음내가 처리하지않고 호출한쪽에서 처리
//,를 해서 여러개를 넘겨줄수 있음
int k = i / j;
return k;
}
}
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(e.toString()); //오류의 정보를 출력
}
}
public static int divide(int i, int j) throws ArithmeticException{
//오류를 처리하지않고 divide를 호출한 쪽에게 오류를 처리하라고 떠넘길수가 있음내가 처리하지않고 호출한쪽에서 처리
//,를 해서 여러개를 넘겨줄수 있음
int k = i / j;
return k;
}
}

Exception 발생시키기
●Exception 발생시키기
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;
}
}
divide메소드는 2번째 파라미터의 값이 0일 경우 나누기를 할 때 Exception이 발생
위의 코드를 에러가 발생하지 않게 수정
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(e.toString());
}
}
public static int divide(int i, int j) throws IllegalArgumentException{
//throws IllegalArgumentException 디바이드를 호출한쪽에서 처리해야됨
if(j == 0){
//오류를 직접 발생시키는 코드
throw new IllegalArgumentException("0으로 나눌수 없습니다");
// throw - 해당 라인에서 exceoption이 발생한다(그줄에서 오류가 발생)
}
int k = i / j;
return k;
}
}

사용자 정의 Exception
●사용자 정의 Exception


출처 : https://school.programmers.co.kr/learn/courses/5/lessons/316
Exception 클래스를 상속 받아 정의한 checked Exception
1.반드시 오류를 처리 해야만 하는 Exception
2.예외 처리하지 않으면 컴파일 오류를 발생 시킴
RuntimeException 클래스를 상속 받아 정의한 unChecked Exception
1.예외 처리하지 않아도 컴파일 시에는 오류를 발생시키지 않음
RuntimeException을 상속받은 BizException객체
public class BizException extends RuntimeException{ //RuntimeException을 상속받음
//어떤 오류가 발생했는지 String값으로 msg를 가지고 들어오는 생성자
//문자열로 된 오류메시지
public BizException(String msg){
super(msg); //부모의 생성자를 호출super
}
//Exception을 받아들여서 해당Exception부모의 생성자에게 넘겨줄수있는 생성자
//실제 발생할 Exception담는 목적의 생성자
public BizException(Exception ex){
super(ex);
}
}
public class BizService {
public void bizMethod(int i ) throws BizException{
System.out.println("비지니스 메소드 시작");
if(i < 0){
throw new BizException("매개변수 i는 0이상이어야 합니다.");
//Exception을 발생시키는 키워드 throw
}
System.out.println("비지니스 메소드 종료");
}
}
public class BizExam {
public static void main(String[] args) {
BizService biz = new BizService();
biz.bizMethod(5);
// biz.bizMethod(-3); //아래코드와 동일
try{
biz.bizMethod(-3);
}catch(Exception ex){
ex.printStackTrace();
}
}
}
출력

5-2주차 후기
오늘 아기사자반의 마지막 시간이였고, 예외처리에 대해 배우는 시간을 가지게되었습니다. 예외처리와 직접예외처리를 만들수있는것에 대해 배웠고, 약간의 복잡함을 느꼇고 그로인해 이해를 아직크게 못한거같지만, 지속적으로 복습을 통해 이해를 해야겠다고 느꼇습니다.