
μ€ν μ€μΈ νλ‘κ·Έλ¨μ΄ μμμΉ λͺ»ν μν©μ μ§λ©΄νμ λ λ°μνλ κ²
Β β«οΈ μμΈκ° λ°μν μ μλ μ½λλ try λΈλ‘ λ΄μ μμΉν΄μΌ νλ©°, ν΄λΉ μμΈλ₯Ό μ²λ¦¬νλ μ½λλ catch λΈλ‘ λ΄μ μμ΄μΌ ν¨.
Β β«οΈ try-catch λΈλ‘ νμ finally λΈλ‘μ μμΉμν¬ μ μμΌλ©°, μμΈ λ°μ μ¬λΆμ μκ΄μμ΄ νμ μ€νλ¨.
Β β«οΈ μ§μ μμΈλ₯Ό λ°μμν΄.
Β β«οΈ λ©μλ μ μΈλΆμ throws ν€μλλ₯Ό μ¬μ©νμ¬ ν΄λΉ λ©μλκ° μμΈλ₯Ό λμ§ μ μλ€λ κ²μ λͺ μν΄μ€.
public class Main {
public static void main(String[] args) {
try {
int result = 10 / 0; // μμΈ λ°μ
} catch (Throwable t) { // νΉμ μ€λ₯κ° μλ Throwable μνΌν΄λμ€λ‘ λͺ¨λ μμΈλ₯Ό μ‘μ tμ λ΄μ
System.out.println("Throwable caught: " + t);
}
}
}
Throwable ν΄λμ€λ λ κ°μ μ£Όμ μλΈ ν΄λμ€, μ¦ Error ν΄λμ€μ Exception ν΄λμ€λ₯Ό κ°μ§κ³ μμ. public class Main {
public static void main(String[] args) {
try {
recursive();
} catch (Error e) {
System.out.println("Error caught: " + e);
}
}
private static void recursive() {
recursive(); // μ¬κ· νΈμΆλ‘ StackOverflowError λ°μ
}
}
import java.io.FileReader;
import java.io.FileNotFoundException;
public class Main {
public static void main(String[] args) {
try {
FileReader fr = new FileReader("non_existing_file.txt");
} catch (Exception e) {
System.out.println("Exception caught: " + e);
}
}
}
IOException, SQLException, ClassNotFoundException, etc.public class Main {
public static void main(String[] args) {
try {
int[] arr = new int[5];
arr[10] = 50;
} catch (RuntimeException e) {
System.out.println("RuntimeException caught: " + e);
}
}
}
NullPointerException, ArrayIndexOutOfBoundsException, ClassCastException, etc.public class Main {
public static void main(String[] args) {
String str = "abc";
try {
int num = Integer.parseInt(str);
} catch (NumberFormatException e) {
System.out.println("μλͺ»λ μ«μ νμμ
λλ€.");
}
}
}
NumberFormatException μμΈ λ°μimport java.io.FileReader;
import java.io.FileNotFoundException;
public class Main {
public static void main(String[] args) {
try {
FileReader fr = new FileReader("non_existing_file.txt");
} catch (FileNotFoundException e) {
System.out.println("νμΌμ μ°Ύμ μ μμ΅λλ€.");
}
}
}
FileNotFoundException, SQLException λ±μ΄ λ°μpublic class Main {
public static void main(String[] args) {
int[] arr = new int[5];
try {
arr[10] = 50;
} catch (ArrayIndexOutOfBoundException e) {
System.out.println("λ°°μ΄μ λ²μλ₯Ό μ΄κ³Όνμμ΅λλ€.");
}
}
}
NullPointerExceptionμ΄ λ°μpublic class Main {
public static void main(String[] args) {
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("0μΌλ‘ λλ μ μμ΅λλ€.");
}
}
}
ArithmeticExceptionμ΄ λ°μν¨class Animal{}
class Dog extends Animal {}
// Dog == Animal
// Animal != Dog
// Dogλ Animalμ μμ ν΄λμ€
public class Main {
public static void main(String[] args) {
Animal a = new Animal();
try {
Dog d = (Dog) a;
} catch (ClassCastException e) {
System.out.println("μλͺ»λ νλ³νμ΄μ΄λ£¨μ΄μ‘μ΅λλ€.");
}
}
}
ClassCastExceptionμ΄ λ°μν¨public class Main {
public static void main(String[] args) {
Object obj = new Object();
try {
obj.notify();
} catch (IllegalMonitorStateException e) {
System.out.println("κ°μ²΄μ λͺ¨λν°λ₯Ό μμ νμ§ μκ³ notifyλ₯Ό νΈμΆνμμ΅λλ€.");
}
}
}