개발 이론/JAVA

Statement와 Exception

jeonghoe21 2025. 1. 30. 19:38

Chapter 1: Statement 개요

Statement Block(문 블록)

자바에서 문 블록은 {} 중괄호로 묶인 코드 집합을 의미하며, 하나의 실행 단위로 동작합니다.

{
    int x = 10;
    System.out.println(x);
}

Statement(문)의 종류

  • 표현식 문(Expression Statement): int x = 5;, System.out.println(x);
  • 선언문(Declaration Statement): int a;
  • 제어문(Control Statement): 조건문, 반복문 등

Chapter 2: Selection Statement(선택 문)

if Statement (if 문)

int num = 10;
if (num > 5) {
    System.out.println("5보다 큽니다");
}

Cascading if statement (Cascading if 문)

int num = 10;
if (num > 10) {
    System.out.println("10보다 큽니다");
} else if (num == 10) {
    System.out.println("10입니다");
} else {
    System.out.println("10보다 작습니다");
}

switch statement(switch 문)

int day = 3;
switch (day) {
    case 1:
        System.out.println("월요일");
        break;
    case 2:
        System.out.println("화요일");
        break;
    default:
        System.out.println("기타 요일");
}

Chapter 3: Iteration Statement(반복 문)

while statement (while 문)

int i = 0;
while (i < 5) {
    System.out.println(i);
    i++;
}

do statement (do 문)

int i = 0;
do {
    System.out.println(i);
    i++;
} while (i < 5);

for statement (for 문)

for (int i = 0; i < 5; i++) {
    System.out.println(i);
}

foreach statement(foreach 문)

int[] numbers = {1, 2, 3, 4, 5};
for (int num : numbers) {
    System.out.println(num);
}

Chapter 4: Jump Statement (분기 문)

goto statement (goto 문)

자바는 goto 문을 지원하지 않습니다. 대신 break, continue, return을 사용합니다.

break와 continue statement(break 문과 continue 문)

for (int i = 0; i < 5; i++) {
    if (i == 3) {
        break;
    }
    System.out.println(i);
}
for (int i = 0; i < 5; i++) {
    if (i == 3) {
        continue;
    }
    System.out.println(i);
}

 

Chapter 5: 기본 예외 처리

예외를 왜 사용해야 하는가?

프로그램 실행 중 오류가 발생했을 때 정상적인 흐름을 유지하기 위해 사용됩니다.

예외 객체

자바의 예외 클래스는 Exception, RuntimeException, Throwable 등이 있습니다.

try/catch 블록

try {
    int result = 10 / 0;
} catch (ArithmeticException e) {
    System.out.println("0으로 나눌 수 없습니다.");
}

다중 catch 블록

try {
    int[] arr = new int[3];
    arr[5] = 10;
} catch (ArrayIndexOutOfBoundsException e) {
    System.out.println("배열 범위를 초과하였습니다.");
} catch (Exception e) {
    System.out.println("예외 발생: " + e.getMessage());
}

Chapter 6: 예외 발생시키기

throw statement (throw 문)

void validateAge(int age) {
    if (age < 18) {
        throw new IllegalArgumentException("미성년자는 이용할 수 없습니다.");
    }
}

메소드에 예외 선언

void readFile() throws IOException {
    FileReader file = new FileReader("test.txt");
}

finally statement (finally 문)

try {
    FileReader file = new FileReader("test.txt");
} catch (IOException e) {
    System.out.println("파일을 읽을 수 없습니다.");
} finally {
    System.out.println("예외 처리 완료");
}

try-with-resource clause (try-with-resource 절)

try (BufferedReader br = new BufferedReader(new FileReader("test.txt"))) {
    System.out.println(br.readLine());
} catch (IOException e) {
    System.out.println("파일 오류: " + e.getMessage());
}

'개발 이론 > JAVA' 카테고리의 다른 글

Collection  (0) 2025.02.02
상속  (0) 2025.02.01
객체지향이란?  (0) 2025.02.01
배열  (0) 2025.02.01
메소드  (0) 2025.01.31