예외 및 에외처리
예외(Exception)
: 프로그램 실행 중 발생하는 오류 중에서 처리가 가능한 것
예외처리(Exception Handling)
: 예외가 발생했을 때 이를 적절히 처리하여 프로그램이 비정상적으로 종료되는 것을 막는 방법
객체로서의 예외
: 자바는 객체지향 언어이기에 예외도 객체로 처리
모든 예외 클래스들은 Exception 클래스를 상속받으므로 Exception으로 처리 가능
예외 클래스들에서 Exception 클래스의 메서드 사용 가능
한번에 모든 예외를 처리하는 방법
try {
예외가 발생할 수 있는 명령;
} catch(Exception e){
예외 발생 시 처리할 명령;
}
throw
: 예외 발생
프로그래머가 고의로 예외를 발생시킬 때 사용하는 방법
구조
Exception e = new Exceiption("Exception");
throw e;
ex.
public class Exception2 {
public static void main(String[] args) {
try {
Exception e = new Exception("고의 예외");
throw e;
} catch (Exception e) {
System.out.println("예외 발생");
System.out.println(e.getMessage());
}
}
}
출력화면
: Exception 생성자 호출 시 전달했던 문자열이 내부적으로 저장되어
객체.getMessage()를 호출하면 출력된다
throws
: 예외 던지기
예외가 발생했을 경우 현재 메서드가 예외를 처리하지 않고
자신을 호출한 쪽으로 예외 처리에 대한 책임을 넘기는 것
구조
void method() throws Exception{
}
예외던지기 시 메서드 선언부에 throws 키워드를 붙여
메서드 호출 하는 부분에서 처리하도록 함
ex1.
public class Exception3 {
public static void main(String[] args) {
try {
methodA();
} catch (Exception e) {
System.out.println("메인에서 처리");
}
}
public static void methodA() throws Exception{
methodB();
}
public static void methodB() throws Exception{
methodC();
}
public static void methodC() throws Exception{
Exception e = new Exception();
throw e; // 예외 발생
}
}
출력화면
ex2.
public class ReThrow {
public static void main(String[] args) {
try {
System.out.println("외부 try");
try {
System.out.println("내부 try");
Exception e = new Exception();
throw e;
} catch (Exception e) {
System.out.println("내부 try-catch exception: " + e);
System.out.println("예외던지기 한 번 더: ");
throw e;
} finally {
System.out.println("finally 구문 출력");
}
} catch (Exception e) {
System.out.println("외부 try catch exception: " + e);
}
System.out.println("종료");
}
}
출력화면
ex3.
public class Exception4 {
public static void main(String[] args) {
int age = -19;
try {
ticketing(age);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void ticketing(int age) throws AgeException{
if(age<0) {
throw new AgeException("잘못된 나이를 입력하셨습니다.");
}
}
}
class AgeException extends Exception{
public AgeException() {
}
public AgeException(String msg) {
super(msg);
}
}
출력화면
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
JAVA Eclipse 29 예외처리 - 예외/예외처리 개념
https://developernew.tistory.com/181
'Backend > JAVA2 멘토시리즈' 카테고리의 다른 글
JAVA Eclipse 29 예외처리 - 예외/예외처리 개념 (0) | 2023.06.27 |
---|---|
JAVA Eclipse 28 내부클래스 (0) | 2023.05.15 |
JAVA Eclipse 27 인터페이스 (0) | 2023.05.12 |
JAVA Eclipse 26 추상메서드와 추상클래스 (0) | 2023.05.11 |
JAVA Eclipse 25 상속과 다형성 - 다형성 개념과 다형성 관련 실습 (1) | 2023.05.11 |