Java

[Java]Exception

siyuning 2022. 6. 22. 16:51

# Exception

  • throw Exception๊ฐ์ฒด์ฃผ์†Œ๊ฐ’
[ex] Exception์ฒ˜๋ฆฌ๋ฅผ ํ•˜์ง€ ์•Š์œผ๋ฉด ์ปดํŒŒ์ผ์—๋Ÿฌ ๋ฐœ์ƒ
class A{
    public static void main(String... args){
    Exception e=new Exception("ํญํƒ„");
    throw e; //throw new Exception();์œผ๋กœ ํ•œ์ค„๋กœ ์“ธ ์ˆ˜ ์žˆ์Œ
    }
}

Exception์ฒ˜๋ฆฌ๋Š” 2๊ฐ€์ง€ ๋ฐฉ์‹์ด ์žˆ๋‹ค.

[an1] ๋ฉ”์„œ๋“œํ˜ธ์ถœํ•œ์ชฝ์œผ๋กœ ๋˜์ง€๊ธฐ

class A {
    public static void main(String... args) throws Exception {
        Exception e = new Exception("ํญํƒ„");
        throw e;
    }
}

[an2] ์˜ˆ์™ธ์ฒ˜๋ฆฌ๊ธฐ ๋งŒ๋“ค๊ธฐ

try{ ์˜ˆ์™ธ๋ฐœ์ƒ๊ฐ€๋Šฅ์†Œ์Šค์ฝ”๋“œ }catch(XXXException ๋ณ€์ˆ˜){ ์˜ˆ์™ธ์ฒ˜๋ฆฌ์†Œ์Šค์ฝ”๋“œ }

class T {
    public static void main(String... args) {
        try {
            Exception e = new Exception("ํญํƒ„");
            throw e;
        } catch (Exception e) {
            System.out.println("์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ถ€๋ถ„");
        }
        System.out.println("์ •์ƒ์‹คํ–‰");
    }
}
//์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ถ€๋ถ„ ์ •์ƒ์‹คํ–‰
//try, catch๋กœ ๋Œ€์‘ํ•˜๋Š” ์ฝ”๋“œ ์‹คํ–‰ and ๋‚˜๋จธ์ง€ ์ฝ”๋“œ ์‹คํ–‰
[ex] ์ปดํŒŒ์ผ์—๋Ÿฌ๋ฅผ ํ•ด๊ฒฐํ•˜๋ ค๋ฉด?
class A{
static void a(){ throw new Exception("ํญํƒ„1"); }
}
class B{
public static void main(String... args){
A.a();
}
}

[an1]
class A{
static void a() throws Exception{ throw new Exception("ํญํƒ„1"); }
}
class B{
public static void main(String... args) throws Exception{
A.a();
}
} //ํญํƒ„1

[an2]
class A{
static void a(){
try{
throw new Exception("ํญํƒ„1");
}catch(Exception e){
System.out.println("์˜ˆ์™ธ์ฒ˜๋ฆฌ");
}
}
}
class B{
public static void main(String... args){
A.a();
}
} //์˜ˆ์™ธ์ฒ˜๋ฆฌ

Exception ์ข…๋ฅ˜

1. CheckedException 
2. UnCheckedException 

[ex] ์ปดํŒŒ์ผ๋‹จ๊ณ„์—์„œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ๊ฒ€์‚ฌํ•˜๋Š” ๊ฒƒ

class A{
public static void main(String... args){
throw new Exception();
}
}

[ex]์ปดํŒŒ์ผ๋‹จ๊ณ„์—์„œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ๊ฒ€์‚ฌํ•˜์ง€ ์•Š๋Š”๊ฒƒ

class A{
public static void main(String... args){
throw new RutimeException();
}
}

[ex]
class A{
static void a(){
// static void a() throws RuntimeException{
throw new RuntimeException();
}
}
class B{
public static void main(String... args){
//public static void main(String... args) throws RuntimeException{
A.a();
}
}
[ex]
class A extends Exception {}
class B extends Exception {}
class C {
    public static void main(String... args) throws A, B {
        switch (args[0]) {
        case "aa":
            throw new A();
        case "bb":
            throw new B();
        }
    }
}
[ex]
class A extends Exception {}
class B extends Exception {}
class C {
    public static void main(String... args) {
        try {
            switch (args[0]) {
            case "aa":
                throw new A();
            case "bb":
                throw new B();
            }
        } catch (A e1) {
            System.out.println("์ฒ˜๋ฆฌ1");
        } catch (B e2) {
            System.out.println("์ฒ˜๋ฆฌ2");
        }
    }
}
[ex] ๋‹ค์ค‘catch
class A {
    public static void main(String... args) {
        try {
            int n1 = Integer.parseInt(args[0]);
            int n2 = Integer.parseInt(args[1]);
            System.out.print(n1 + "+" + n2 + "=" + (n1 + n2));
        } catch (NumberFormatException e1) {
            System.out.println("์ˆซ์žํ˜•์‹์˜ค๋ฅ˜");
        } catch (ArrayIndexOutOfBoundsException e2) {
            System.out.println("์ˆซ์ž ๋‘๊ฐœ๋ฅผ ๋ฐ˜๋“œ์‹œ ์ž…๋ ฅํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.");
        }
    }
}
[ex] java.io.FileInputStreamํด๋ž˜์Šค ๊ฐ์ฒด์ƒ์„ฑ ์†Œ์Šค์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”.

[an1]
import [java.io](http://java.io/).*;
class A {
    public static void main(String... args) throws FileNotFoundException {
        FileInputStream o = new FileInputStream("aaa");
    }
}

[an2]
import [java.io](http://java.io/).*;
class A {
    public static void main(String... args) {
        try {
            FileInputStream o = new FileInputStream("a.java");
        } catch (FileNotFoundException e) {
            System.out.println("ํŒŒ์ผ์—†์–ด์š”!");
        }
    }
}