Java 예외

예외의 처리

수업소개

자바에서 예외가 발생했을 때 이를 처리하는 방법을 소개합니다. 

 

 

 

강의

 

 

 

소스코드

https://github.com/egoing/java-exception/commit/2812c9af98d93bc9039cf224f32a825ff4b15caa

public class ExceptionApp {
    public static void main(String[] args) {
        System.out.println(1);
        int[] scores = {10,20,30};
        try {
            System.out.println(2);
            System.out.println(scores[3]);
            System.out.println(3);
            System.out.println(2 / 0);
            System.out.println(4);
        } catch(ArithmeticException e){
            System.out.println("잘못된 계산이네요.");
        } catch(ArrayIndexOutOfBoundsException e){
            System.out.println("없는 값을 찾고 계시네요 ^^");
        }
        System.out.println(5);
    }
}

 

댓글

댓글 본문
  1. 당당
    2023.04.17
  2. Min Jupiter
    23.01.03
  3. 코딩이취미다
    예외 상황에 따라 별도로 처리를 해야 하나요?
    모든 예외를 처리 하는게 있을 것 같은데...
    있겠지....
  4. 나연
    2022년 2월 26일 (토) 완료

    ```java
    try {
    System.out.println(2 / 0);
    } catch (ArithmeticException e) {
    // e는 변수. 보통 "e"라고 이름 지음.
    System.out.println("Wrong calculation.");
    }
    ```
    - `System.out.println(2 / 0);` 실행을 시도한 후 `ArithmeticException`이 발생하면 `catch (ArithmeticException e)` 중괄호 안에 있는 코드 실행
    - `try` / `catch`를 사용하지 않으면 익셉션이 발생했을 때 거기서 프로그램이 멈춤
  5. 2021.08.31 완료
  6. oyuiw
    20201213
graphittie 자세히 보기