JAVA Exception



Checked Exception

Exception 처리를 하지 않은 경우

Untitled

Untitled

package sec01.exam1;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class tutorial {
    public static void main(String[] args) {
        checkedExceptionWithThrows();
    }

    private static void checkedExceptionWithThrows() {
        File file = new File("not_existing_file.txt");
        FileInputStream stream = new FileInputStream(file);
    }


Exception 처리를 한 경우

Untitled

package sec01.exam1;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class tutorial {
    public static void main(String[] args) {
        checkedExceptionWithTryCatch();
    }

    private static void checkedExceptionWithTryCatch() {
        File file = new File("not_existing_file.txt");
        try {
            FileInputStream stream = new FileInputStream(file);
            System.out.println("File opened successfully.");
        } catch (FileNotFoundException e) {
            System.err.println("File not found: " + e.getMessage());
            e.printStackTrace();
        }
    }
}