์์ธ์ฒ๋ฆฌ
1. ๊ฐ๋
์์ธ ๋ฐ์ํ ๊ฒ์ ๋๋นํด์ ํ๋ก๊ทธ๋จ์์ ์ ์ดํ ์ ์๋๋ก ์ฒ๋ฆฌํ๋ ๊ฐ๋
์ค๋ฅ์๋ ๋ค๋ฅด๋ฉฐ ํ๋ก๊ทธ๋จ ์ํ ๋์ค์ ๋ฐ์ํ ์ ์๋ ์ฅ์ ์ ๋ํ ๋์ฒ
2. ํน์ง
- try~catch~finally ๊ตฌ๋ฌธ์ผ๋ก ์ ์ด
- ์์ธ ๋๊ธฐ๊ธฐ ์ํด throws ์ฌ์ฉ
3. ์์ธ์ฒ๋ฆฌ ํค์๋
- try ๋ธ๋ก : ์์ธ๊ฐ ๋ฐ์ํ ์ ์๋ ์ฝ๋ ํฌํจ
- catch ๋ธ๋ก : try ๋ธ๋ก ๋ด์์ ๋ฐ์ํ ์์ธ๋ฅผ ์ฒ๋ฆฌ
- finally : ์์ธ ๋ฐ์ ์ฌ๋ถ์ ์๊ด ์์ด ์คํ๋์ด์ผ ํ๋ ์ฝ๋๋ฅผ ํฌํจ
- throw : ํ๋ก๊ทธ๋จ์์ ์ง์ ์์ธ๋ฅผ ๋ฐ์์ํค๊ธฐ ์ํด ์ฌ์ฉ
- throws : ํด๋น ๋ฉ์๋์์ ์ฒ๋ฆฌํ์ง ์๊ณ ํธ์ถ์์๊ฒ ์์ธ ์ฒ๋ฆฌ ์์
4. ์์ธ์ฒ๋ฆฌ ๋ฐฉ์
try {
// ์์ธ๊ฐ ๋ฐ์๋ ๋งํ ์ฝ๋
}
catch(FileNotFoundException e) {
// FileNotFoundException์ด ๋ฐ์ํ๋ค๋ฉด ์คํ
}
catch(IOE Exception e) {
// IOException์ด ๋ฐ์ํ์๋ค๋ฉด ์คํ
}
catch(Exception e) {
// FileNotFoundException, IOException ์ธ์ Exception ๋ฐ์ํ๋ฉด ์คํ
} finally {
// ์์ธ์ ์๊ด์์ด ๋ฌด์กฐ๊ฑด ์ค๋ธํ
// ์์ธ์ ๋๋จธ์ง ์ฒ๋ฆฌ, ์์ ํด์ ์ฉ๋
}
5. ์์ฃผ ์ฌ์ฉ๋๋ ์์ธ ํด๋์ค
์ข ๋ฅ | ์ค๋ช |
NullPointerException | ๊ฐ์ฒด๋ฅผ ์์ฑํ์ง ์๊ณ ์ฐธ์กฐํ ๊ฒฝ์ฐ ํด๋น ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ ์ ์์ด์ ๋ฐ์ |
InderOutOfBoundsException | ๋ฐฐ์ด์์ ์ธ๋ฑ์ค ๋ฒ์๋ฅผ ์ด๊ณผํ์ ๋ ๋ฐ์ |
NumberFormatException | ๋ฌธ์์ด์ ์ซ์๋ก ๋ณํํ ๋ ํด๋น ๋ฌธ์๊ฐ ์ซ์ ํํ๊ฐ ์๋๋ผ๋ฉด ๋ฐ์ |
ClassCastException | ํ์ฉ๋์ง ์์ ํ์ ์ผ๋ก ๋ณํํ ๋ ๋ฐ์ |
IllegalArgumentException | ๋งค๊ฐ๋ณ์๊ฐ ์๋ํ์ง ์์ ์ํฉ์ ์ ๋ฐํ ๋ ๋ฐ์ |
ArithmeticException | ์ฐ์ ์ฐ์ฐ์ ์ค๋ฅ ์์ ๋ ๋ฐ์(์: 0์ผ๋ก ๋๋ ๊ฒฝ์ฐ) |
6. ๊ฐ๋จํ ์์
public class ExceptionExample {
public static void main(Strig[] args) {
try {
int[] numbers = {1, 2, 3};
System.out.println(numbers[3]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("๋ฐฐ์ด์ ๋ฒ์๋ฅผ ๋ฒ์ด๋ ์ ๊ทผ์
๋๋ค.");
} finally {
System.out.println("์ด ์ฝ๋๋ ํญ์ ์คํ๋ฉ๋๋ค.");
}
}
}
์์ธ์ฒ๋ฆฌ ๋ฌธ์ 1
class ExceptionExample {
public static void p() throws Exception {
try {
System.out.println("A");
throw new Exception("");
} catch (Exception e) {
System.out.println("C");
throw e;
} finally {
System.out.println("D");
}
}
public static void main(String[] args) {
try {
System.out.println("A-1");
p();
System.out.println("B-1");
} catch (Exception e) {
System.out.println("C-1");
} finally {
System.out.print("D-1");
}
}
}
์ถ๋ ฅ๊ฒฐ๊ณผ
A-1
A
C
D
C-1
D-1
์์ธ์ฒ๋ฆฌ ๋ฌธ์ 2
class ExceptionExample {
public static void p() throws Exception {
try {
System.out.println("A");
throw new Exception("");
} catch (Exception e) {
System.out.println("C");
} finally {
System.out.println("D");
}
}
public static void main(String[] args) {
try {
System.out.print("A-1");
p();
System.out.println("B-1");
} catch (Exception e) {
System.out.println("C-1");
} finally {
System.out.println("D-1");
}
}
}
์ถ๋ ฅ ๊ฒฐ๊ณผ
A-1
A
C
D
B-1
D-1
์์ธ์ฒ๋ฆฌ ๋ฌธ์ 3
class ExceptionTest {
ExceptionTest() {
try {
method();
System.out.print("A");
} catch(Exception e) {
System.out.print("B");
} finally {
System.out.print("C");
}
System.out.println("D");
}
void method() throws Exception {
throw new Exception();
}
public static void main(String[] args) {
ExceptionTest t = new ExceptionTest();
}
}
์ถ๋ ฅ ๊ฒฐ๊ณผ
B C D
์์ธ์ฒ๋ฆฌ ๋ฌธ์ 4
class TextException {
public static void main(String[] args) {
try {
System.out.println("๋ฌธ์ฅ A");
foo();
System.out.println("๋ฌธ์ฅ B");
} catch(Exception e) {
System.out.println("๋ฌธ์ฅ C");
}
System.out.println("๋ฌธ์ฅ D");
}
public static void foo() throws Exception {
try {
System.out.println("๋ฌธ์ฅ E");
throw new Exception();
} catch (Exception e) {
System.out.println("๋ฌธ์ฅ F");
throw e;
} finally {
System.out.println("๋ฌธ์ฅ G");
}
}
}
์ถ๋ ฅ ๊ฒฐ๊ณผ
๋ฌธ์ฅ A
๋ฌธ์ฅ E
๋ฌธ์ฅ F
๋ฌธ์ฅ G
๋ฌธ์ฅ C
๋ฌธ์ฅ D
์์ธ์ฒ๋ฆฌ ๋ฌธ์ 5
class TextException {
public static void main(String[] args) {
try {
System.out.print("A");
foo();
System.out.print("B"); // This will not execute because foo() throws an exception
} catch (Exception e) {
System.out.print("C");
} finally {
System.out.println("E");
}
}
public static void foo() throws Exception {
try {
System.out.print("F");
throw new Exception();
} catch (Exception e) {
System.out.print("G");
} finally {
System.out.print("H");
}
}
}
์ถ๋ ฅ ๊ฒฐ๊ณผ
AFGHBD
E
'ETC > ์ ๋ณด์ฒ๋ฆฌ๊ธฐ์ฌ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์์ธ์ฒ๋ฆฌ
1. ๊ฐ๋
์์ธ ๋ฐ์ํ ๊ฒ์ ๋๋นํด์ ํ๋ก๊ทธ๋จ์์ ์ ์ดํ ์ ์๋๋ก ์ฒ๋ฆฌํ๋ ๊ฐ๋
์ค๋ฅ์๋ ๋ค๋ฅด๋ฉฐ ํ๋ก๊ทธ๋จ ์ํ ๋์ค์ ๋ฐ์ํ ์ ์๋ ์ฅ์ ์ ๋ํ ๋์ฒ
2. ํน์ง
- try~catch~finally ๊ตฌ๋ฌธ์ผ๋ก ์ ์ด
- ์์ธ ๋๊ธฐ๊ธฐ ์ํด throws ์ฌ์ฉ
3. ์์ธ์ฒ๋ฆฌ ํค์๋
- try ๋ธ๋ก : ์์ธ๊ฐ ๋ฐ์ํ ์ ์๋ ์ฝ๋ ํฌํจ
- catch ๋ธ๋ก : try ๋ธ๋ก ๋ด์์ ๋ฐ์ํ ์์ธ๋ฅผ ์ฒ๋ฆฌ
- finally : ์์ธ ๋ฐ์ ์ฌ๋ถ์ ์๊ด ์์ด ์คํ๋์ด์ผ ํ๋ ์ฝ๋๋ฅผ ํฌํจ
- throw : ํ๋ก๊ทธ๋จ์์ ์ง์ ์์ธ๋ฅผ ๋ฐ์์ํค๊ธฐ ์ํด ์ฌ์ฉ
- throws : ํด๋น ๋ฉ์๋์์ ์ฒ๋ฆฌํ์ง ์๊ณ ํธ์ถ์์๊ฒ ์์ธ ์ฒ๋ฆฌ ์์
4. ์์ธ์ฒ๋ฆฌ ๋ฐฉ์
try { // ์์ธ๊ฐ ๋ฐ์๋ ๋งํ ์ฝ๋ } catch(FileNotFoundException e) { // FileNotFoundException์ด ๋ฐ์ํ๋ค๋ฉด ์คํ } catch(IOE Exception e) { // IOException์ด ๋ฐ์ํ์๋ค๋ฉด ์คํ } catch(Exception e) { // FileNotFoundException, IOException ์ธ์ Exception ๋ฐ์ํ๋ฉด ์คํ } finally { // ์์ธ์ ์๊ด์์ด ๋ฌด์กฐ๊ฑด ์ค๋ธํ // ์์ธ์ ๋๋จธ์ง ์ฒ๋ฆฌ, ์์ ํด์ ์ฉ๋ }
5. ์์ฃผ ์ฌ์ฉ๋๋ ์์ธ ํด๋์ค
์ข ๋ฅ | ์ค๋ช |
NullPointerException | ๊ฐ์ฒด๋ฅผ ์์ฑํ์ง ์๊ณ ์ฐธ์กฐํ ๊ฒฝ์ฐ ํด๋น ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ ์ ์์ด์ ๋ฐ์ |
InderOutOfBoundsException | ๋ฐฐ์ด์์ ์ธ๋ฑ์ค ๋ฒ์๋ฅผ ์ด๊ณผํ์ ๋ ๋ฐ์ |
NumberFormatException | ๋ฌธ์์ด์ ์ซ์๋ก ๋ณํํ ๋ ํด๋น ๋ฌธ์๊ฐ ์ซ์ ํํ๊ฐ ์๋๋ผ๋ฉด ๋ฐ์ |
ClassCastException | ํ์ฉ๋์ง ์์ ํ์ ์ผ๋ก ๋ณํํ ๋ ๋ฐ์ |
IllegalArgumentException | ๋งค๊ฐ๋ณ์๊ฐ ์๋ํ์ง ์์ ์ํฉ์ ์ ๋ฐํ ๋ ๋ฐ์ |
ArithmeticException | ์ฐ์ ์ฐ์ฐ์ ์ค๋ฅ ์์ ๋ ๋ฐ์(์: 0์ผ๋ก ๋๋ ๊ฒฝ์ฐ) |
6. ๊ฐ๋จํ ์์
public class ExceptionExample { public static void main(Strig[] args) { try { int[] numbers = {1, 2, 3}; System.out.println(numbers[3]); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("๋ฐฐ์ด์ ๋ฒ์๋ฅผ ๋ฒ์ด๋ ์ ๊ทผ์
๋๋ค."); } finally { System.out.println("์ด ์ฝ๋๋ ํญ์ ์คํ๋ฉ๋๋ค."); } } }
์์ธ์ฒ๋ฆฌ ๋ฌธ์ 1
class ExceptionExample { public static void p() throws Exception { try { System.out.println("A"); throw new Exception(""); } catch (Exception e) { System.out.println("C"); throw e; } finally { System.out.println("D"); } } public static void main(String[] args) { try { System.out.println("A-1"); p(); System.out.println("B-1"); } catch (Exception e) { System.out.println("C-1"); } finally { System.out.print("D-1"); } } }
์ถ๋ ฅ๊ฒฐ๊ณผ
A-1 A C D C-1 D-1
์์ธ์ฒ๋ฆฌ ๋ฌธ์ 2
class ExceptionExample { public static void p() throws Exception { try { System.out.println("A"); throw new Exception(""); } catch (Exception e) { System.out.println("C"); } finally { System.out.println("D"); } } public static void main(String[] args) { try { System.out.print("A-1"); p(); System.out.println("B-1"); } catch (Exception e) { System.out.println("C-1"); } finally { System.out.println("D-1"); } } }
์ถ๋ ฅ ๊ฒฐ๊ณผ
A-1 A C D B-1 D-1
์์ธ์ฒ๋ฆฌ ๋ฌธ์ 3
class ExceptionTest { ExceptionTest() { try { method(); System.out.print("A"); } catch(Exception e) { System.out.print("B"); } finally { System.out.print("C"); } System.out.println("D"); } void method() throws Exception { throw new Exception(); } public static void main(String[] args) { ExceptionTest t = new ExceptionTest(); } }
์ถ๋ ฅ ๊ฒฐ๊ณผ
B C D
์์ธ์ฒ๋ฆฌ ๋ฌธ์ 4
class TextException { public static void main(String[] args) { try { System.out.println("๋ฌธ์ฅ A"); foo(); System.out.println("๋ฌธ์ฅ B"); } catch(Exception e) { System.out.println("๋ฌธ์ฅ C"); } System.out.println("๋ฌธ์ฅ D"); } public static void foo() throws Exception { try { System.out.println("๋ฌธ์ฅ E"); throw new Exception(); } catch (Exception e) { System.out.println("๋ฌธ์ฅ F"); throw e; } finally { System.out.println("๋ฌธ์ฅ G"); } } }
์ถ๋ ฅ ๊ฒฐ๊ณผ
๋ฌธ์ฅ A ๋ฌธ์ฅ E ๋ฌธ์ฅ F ๋ฌธ์ฅ G ๋ฌธ์ฅ C ๋ฌธ์ฅ D
์์ธ์ฒ๋ฆฌ ๋ฌธ์ 5
class TextException { public static void main(String[] args) { try { System.out.print("A"); foo(); System.out.print("B"); // This will not execute because foo() throws an exception } catch (Exception e) { System.out.print("C"); } finally { System.out.println("E"); } } public static void foo() throws Exception { try { System.out.print("F"); throw new Exception(); } catch (Exception e) { System.out.print("G"); } finally { System.out.print("H"); } } }
์ถ๋ ฅ ๊ฒฐ๊ณผ
AFGHBD E