2025-03-10
try-catch
, throws
, throw
λ±μ νμ©νμ¬ κ΅¬νν¨.μμΈ μ ν | μ€λͺ | μμ |
---|---|---|
Checked Exception | μ»΄νμΌ νμμ 체ν¬λλ μμΈ | IOException , SQLException |
Unchecked Exception | λ°νμ(μ€ν μ€) λ°μνλ μμΈ | NullPointerException , ArrayIndexOutOfBoundsException |
μ¬μ©μ μ μ μμΈ | μ§μ μ μν μμΈ ν΄λμ€ | class MyException extends Exception {} |
try-catch
λΈλ‘ μ¬μ©public class ExceptionHandling {
public static void main(String[] args) {
try {
int num = 10 / 0; // 0μΌλ‘ λλκΈ° μλ (μμΈ λ°μ)
} catch (ArithmeticException e) { // μμΈ μ²λ¦¬
System.out.println("μμΈ λ°μ: " + e.getMessage());
} finally {
System.out.println("μμΈ μ²λ¦¬ ν μ€νλλ finally λΈλ‘");
}
}
}
μμΈ λ°μ: / by zero
μμΈ μ²λ¦¬ ν μ€νλλ finally λΈλ‘
π‘ finally
λΈλ‘μ μμΈ λ°μ μ¬λΆμ μκ΄μμ΄ μ€νλ¨!
throws
ν€μλ μ¬μ© (μμΈ λ λκΈ°κΈ°)import java.io.*;
public class ThrowsExample {
public static void readFile() throws IOException {
BufferedReader br = new BufferedReader(new FileReader("test.txt"));
System.out.println(br.readLine());
br.close();
}
public static void main(String[] args) {
try {
readFile();
} catch (IOException e) {
System.out.println("νμΌ μ½κΈ° μ€ μμΈ λ°μ: " + e.getMessage());
}
}
}
π‘ throws
λ λ©μλκ° μμΈλ₯Ό μ§μ μ²λ¦¬νμ§ μκ³ νΈμΆν κ³³μΌλ‘ λκΉ!
class MyException extends Exception { // μ¬μ©μ μ μ μμΈ ν΄λμ€
public MyException(String message) {
super(message);
}
}
public class CustomExceptionExample {
public static void main(String[] args) {
try {
throw new MyException("μ¬μ©μ μ μ μμΈ λ°μ!");
} catch (MyException e) {
System.out.println("μμΈ λ°μ: " + e.getMessage());
}
}
}
μμΈ λ°μ: μ¬μ©μ μ μ μμΈ λ°μ!
π‘ νΉμ μν©μμ μ§μ μμΈλ₯Ό μ μνμ¬ μ¬μ©ν μ μμ!
Object
νλ³ν μ κ±°.package ch16;
// μ λ€λ¦μ μ¬μ©νμ¬ λ€μν νμ
μ μ²λ¦¬ν μ μλ ν΄λμ€
class Box<T> {
private T item;
public Box(T item) {
this.item = item;
}
public T getItem() {
return item;
}
@Override
public String toString() {
return "Box contains: " + item;
}
}
public class GenericExample {
public static void main(String[] args) {
Box<String> stringBox = new Box<>("Hello"); // String νμ
μ¬μ©
Box<Integer> intBox = new Box<>(123); // Integer νμ
μ¬μ©
System.out.println(stringBox);
System.out.println(intBox);
}
}
Box contains: Hello
Box contains: 123
π‘ μ λ€λ¦μ νμ©νλ©΄ λ€μν λ°μ΄ν° νμ μ νλμ ν΄λμ€μμ μ²λ¦¬ν μ μμ!
package ch16;
// νΉμ μμ ν΄λμ€λ₯Ό μμλ°μ ν΄λμ€λ§ νμ©νλ μ λ€λ¦
class Fruit {}
class Apple extends Fruit {
@Override
public String toString() {
return "Apple";
}
}
class Banana extends Fruit {
@Override
public String toString() {
return "Banana";
}
}
// Tλ Fruitμ μμλ°μ ν΄λμ€λ§ κ°λ₯
class FruitBox<T extends Fruit> {
private T fruit;
public FruitBox(T fruit) {
this.fruit = fruit;
}
@Override
public String toString() {
return "FruitBox contains: " + fruit;
}
}
public class GenericBoundExample {
public static void main(String[] args) {
FruitBox<Apple> appleBox = new FruitBox<>(new Apple());
FruitBox<Banana> bananaBox = new FruitBox<>(new Banana());
System.out.println(appleBox);
System.out.println(bananaBox);
}
}
FruitBox contains: Apple
FruitBox contains: Banana
π‘ μ λ€λ¦μ extends
λ₯Ό μ¬μ©νλ©΄ νΉμ νμ
λ§ νμ©ν μ μμ!
try-catch-finally
λ₯Ό μ¬μ©νμ¬ μμΈλ₯Ό μ²λ¦¬ν μ μμΌλ©°, throw
μ throws
λ₯Ό νμ©νμ¬ μμΈλ₯Ό λμ§ μλ μμ.extends
λ₯Ό νμ©νμ¬ νΉμ ν΄λμ€λ§ μ λ€λ¦ νμ
μΌλ‘ νμ©ν μλ μμ.