왕현성
코딩발자취
왕현성
전체 방문자
오늘
어제
  • 코딩 (277)
    • Python (71)
    • Java (16)
    • MySQL (34)
    • 인공지능 (48)
      • 머신러닝 (16)
      • 딥러닝 (32)
    • 영상처리 (4)
    • Rest API (21)
    • Android Studio (25)
    • streamlit (13)
    • DevOps (22)
      • AWS (9)
      • PuTTY (5)
      • Git (4)
      • Serverless (2)
      • Docker (2)
    • IT 기술 용어 (6)
    • 디버깅 ( 오류 해결 과정 ) (17)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • PYTHON
  • 영상기술
  • matplotlib
  • maskimage
  • labelme UnocodeDecodeError
  • TensorFlow
  • 영상처리역사
  • labelme
  • encoding='utf-8'
  • yolov8
  • ckpt_file
  • imageprocessing
  • unsupervised
  • 의료이미징
  • numpy
  • 영상처리
  • 비지도학습
  • pip install labelme
  • tune()
  • pytorch
  • 컴퓨터비전
  • get_long_description
  • OpenCV
  • 기상탐사
  • alibi-detection
  • UnboundLocalError
  • PIL
  • ComputerVision
  • alibidetect
  • 딥러닝

최근 댓글

최근 글

티스토리

250x250
hELLO · Designed By 정상우.
왕현성

코딩발자취

Java

Java - 예외 처리 - Try/Catch/Finally

2023. 1. 26. 12:18
728x90

1. 예외 처리

  • 자바에서 문법적 오류나 논리적 오류가 발생 할 경우, 해당 예외를 처리하는 구문
  • 오류가 발생 할 것 같은 부분에서 Try로 정의
  • 오류가 발생 할 경우 수행 할 코드를 Catch로 정의

 

1.1. Try/Catch를 이용한 예외 처리 예시

 

int[] arr = {1, 2, 3};

try { // 오류가 발생 할 것 같은 부분에 정의
    for (int i=0; i<4; i++) { // 배열의 범위 초과
        System.out.println( arr[i] );
    }
}
catch (Exception e) { // 오류가 발생하면 수행 할 코드 정의
    System.out.println("오류 발생 : ");
}
// 오류 발생 : java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3

2. 예외 객체

  • 예외 객체를 이용하여 상황에 따른 대처
  • 예외 처리로 반환되는 오류의 이름을 적어주면 해당 오류 발생시 수행 코드 정의 가능
int[] arr = {1, 2, 3};

try {
    for (int i=0; i<4; i++) {
        System.out.println( arr[i] );
    }
}
catch (ArrayIndexOutOfBoundsException ArrayError) {
    System.out.println("배열 오류 : " + ArrayError);
}
catch (Exception e) {
    System.out.println("오류 발생 : " + e);
}
// 배열 오류 : java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3

3. Finally

  • 예외 처리의 유무 상관없이 반드시 수행 할 코드를 정의

 

3.1. 오류가 발생되는 예외 처리 구문

int[] arr = {1, 2, 3};

try {
    for (int i=0; i<4; i++) { // 배열 범위 초과로 오류 발생
        System.out.println( arr[i] );
    }
}
catch (Exception e) {
    System.out.println("오류 발생 : " + e);
}
finally {
    System.out.println("오류 발생 유무 상관없이 반드시 수행된다.");
}
// 오류 발생 : java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
// 오류 발생 유무 상관없이 반드시 수행된다.

3.2. 오류가 발생되지 않는 예외 처리 구문

int[] arr = {1, 2, 3};

try {
    for (int i=0; i<3; i++) { // 배열 범위 정상적으로 입력, 오류 발생하지 않음
        System.out.println( arr[i] );
    }
}
catch (Exception e) {
    System.out.println("오류 발생 : " + e);
}
finally {
    System.out.println("오류 발생 유무 상관없이 반드시 수행된다.");
}
// 123
// 오류 발생 유무 상관없이 반드시 수행된다.

'Java' 카테고리의 다른 글

java - email 체크 정규식  (0) 2023.01.31
Java - ArrayList와 HashMap / size(), set(), remove(), clear(), isEmpty,put(),replace(),  (0) 2023.01.25
Java - 정수 및 실수를 클래스로 생성하는 방법 / 문자열 형변환 / 문자열 함수  (0) 2023.01.25
Java - 추상클래스 (abstract)와 인터페이스(interface)  (0) 2023.01.20
Java - 상속 (Inheritance) / 메소드 오버라이딩 (Method Overriding) / super  (0) 2023.01.19
    'Java' 카테고리의 다른 글
    • java - email 체크 정규식
    • Java - ArrayList와 HashMap / size(), set(), remove(), clear(), isEmpty,put(),replace(),
    • Java - 정수 및 실수를 클래스로 생성하는 방법 / 문자열 형변환 / 문자열 함수
    • Java - 추상클래스 (abstract)와 인터페이스(interface)
    왕현성
    왕현성
    AI 머신비전 학습일지

    티스토리툴바