Backend/JAVA2 멘토시리즈

JAVA Eclipse 29 예외처리 - 예외/예외처리 개념

쏠솔랄라 2023. 6. 27. 08:56

 

 

예외(Exception)

 

: 프로그램 실행 중 발생하는 오류 중에서 처리가 가능한 것

 

 

에러

: 개발자가 조치를 취할 수 없는 수준 ex.  메모리 부족, JVM 동작 이상

 

컴파일 에러

: 컴파일 시 발생하는 에러 ex. 오타, 잘못된 자료형 등

 

런타임 에러

: 프로그램 실행 도중에 발생하는 에러 ex. 정수값 대신 문자열 입력 등

 

로직 에러

: 실행은 되지만 의도와는 다르게 동작하는 에러

 

예외

: 다른 방식으로 처리 가능한 오류 ex. 입력값 오류, 네트워크 문제

 

 

 

예외처리(Exception Handling)

 

: 예외가 발생했을 때 이를 적절히 처리하여 프로그램이 비정상적으로 종료되는 것을 막는 방법

ex. 입력 오류: 숫자를 입력해야 하는 곳에 문자를 입력할 경우를 방지하기 위해

사용자에게 문지 입력 시 숫자를 입력해야 한다고 알려줌

 

 


 

 

try - catch

 

: 예외 발생 시, 적절하게 처리하기 위해 자바에서 제공하는 예외처리 문법

 

 

try{
     예외가 발생할 수 있는 명령;
} catch (발생할 수 있는 예외 클래스명) {
     예외 발생 시 실행할 명령
}

 

 

ex.

public class Exception {
	
	public static void main(String[] args) {
		
		try {
			int []a = {2,0, 0};
			int b=4;
			int c=b/a[2];
			System.out.println(c);
		} catch (ArithmeticException e) {
			// 산술적 오류
			System.out.println("산술 오류 발생");
		} catch (ArrayIndexOutOfBoundsException e) {
			// 배열에 접근하는 인덱스 값의 오류
			System.out.println("배열 길이 오류 발생");
		}
		System.out.println("예외처리 공부 중");
	}

}

 

출력화면

 

 

try - catch - finally

 

: try - catch문에 선택적으로 추가할 수 있는 문법으로

오류가 발생하든 발생하지 않든 무조건 실행하는 구문

 

 

try {
     예외가 발생할 수 있는 명령;
} catch (발생할 수 있는 예외 클래스명) {
     예외 발생 시 실행할 명령;
} finally {
     예외가 발생하든 안하든 무조건 실행하는 명령;
}

 

 

ex.

public class Finally {
	
	public static void main(String[] args) {
		
		// 외부로 접근
		
		int a=0;
		int b=2;
		
		try {
			System.out.println("예외로 접속");
			int c=b/a;
		} catch (ArithmeticException e) {
			System.out.println("오류가 발생");
		} finally {
			System.out.println("무조건 연결 해제");
		}
		
	}

}

 

출력화면

 

public class Finally {
	
	public static void main(String[] args) {
		
		// 외부로 접근
		
		int a=3;
		int b=2;
		
		try {
			System.out.println("예외로 접속");
			int c=b/a;
		} catch (ArithmeticException e) {
			System.out.println("오류가 발생");
		} finally {
			System.out.println("무조건 연결 해제");
		}
		
	}

}

 

int a의 값을 3으로 변경 시 구문 에러가 없어지므로

오류는 발생하지 않지만 연결은 무조건 해제함

 

출력화면

 

 


 

 

JAVA Eclipse 01 프로그램, 프로그래밍, 기계어, JAVA
https://developernew.tistory.com/63

JAVA Eclipse 02 자바 출력메서드와 입력메서드
https://developernew.tistory.com/71

JAVA Eclipse 03 변수, 자료형, 형변환, 변수의 상수화
https://developernew.tistory.com/74

JAVA Eclipse 04 연산자 정의, 연산자 종류, 연산자 우선순위
https://developernew.tistory.com/78

JAVA Eclipse 05 논리연산자, 비트연산자
https://developernew.tistory.com/80

JAVA Eclipse 06 기타연산자 - 삼항연산자, 대입연산자, 복합대입연산자, instanceof
https://developernew.tistory.com/84

JAVA Eclipse 07 제어문 : 조건문
https://developernew.tistory.com/88

JAVA Eclipse 08 제어문 : 조건문 switch + Random 클래스
https://developernew.tistory.com/93

JAVA Eclipse 09 제어문 : 반복문 for
https://developernew.tistory.com/102

JAVA Eclipse 10 제어문 : 반복문 while, do-while
https://developernew.tistory.com/103

JAVA Eclipse 11 제어문 : 반복문의 break, continue
https://developernew.tistory.com/106

JAVA Eclipse 12 배열 : 배열의 개념 및 사용
https://developernew.tistory.com/107

JAVA Eclipse 13 배열 : 예제풀이, 로또번호 생성기
https://developernew.tistory.com/115

JAVA Eclipse 14 배열 : 다차원배열
https://developernew.tistory.com/117

JAVA Eclipse 15 배열의 복제, for ~ each문
https://developernew.tistory.com/120

JAVA Eclipse 16 카페 주문 시스템(키오스크) 배열과 제어문(반복문, 조건문)으로 풀기
https://developernew.tistory.com/124

JAVA Eclipse 17 method 메서드(메소드)
https://developernew.tistory.com/126

JAVA Eclipse 18 메서드(메소드) 오버로딩
https://developernew.tistory.com/133

JAVA Eclipse 19 객체지향 언어
https://developernew.tistory.com/135

JAVA Eclipse 20 클래스와 객체
https://developernew.tistory.com/155

JAVA Eclipse 21 인스턴스 변수와 클래스 변수
https://developernew.tistory.com/156

JAVA Eclipse 22 객체 타입 배열
https://developernew.tistory.com/157

JAVA Eclipse 23 클래스 생성자
https://developernew.tistory.com/158

JAVA Eclipse 24 상속과 다형성 - 상속/메서드 오버라이딩
https://developernew.tistory.com/159

JAVA Eclipse 25 상속과 다형성 - 다형성 개념과 다형성 관련 실습
https://developernew.tistory.com/160

JAVA Eclipse 26 추상메서드와 추상클래스
https://developernew.tistory.com/161

JAVA Eclipse 27 인터페이스
https://developernew.tistory.com/162

JAVA Eclipse 28 내부클래스
https://developernew.tistory.com/166