: ์ฝ๋ ์์ฒด๋ฅผ ์๋ชป ์์ฑํ ๊ฒ
: ์คํ ์์ ๋ฐ์ํ๋ ์๋ฌ
try : Exception์ด ๋ฐ์ํ ์ ์๋ ๊ตฌ๋ฌธ์ ๋ฃ๋๋ค.
catch : ๋ง๋ค์ด์ง Exception ๊ฐ์ฒด๋ฅผ ์ก์์ ์ฒ๋ฆฌํ๋ค.
finally : ์ค๋ฅ๊ฐ ์๊ฑด ์๊ฑด ๋ฌด์กฐ๊ฑด ์คํ๋๋ค.
package lecture0714;
public class Main {
public static void main(String[] args) {
System.out.println("ํ๋ก๊ทธ๋จ ์์!");
int result = 10 / 0; // ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค.
System.out.println("ํ๋ก๊ทธ๋จ ์ข
๋ฃ");
}
}
package lecture0714;
public class Main {
public static void main(String[] args) {
System.out.println("ํ๋ก๊ทธ๋จ ์์!");
try { // exception์ด ๋ฐ์ํ ์ ์๋ ์ฝ๋
int result = 10 / 0;
} catch(ArithmeticException e) {
// exception์ ์ฒ๋ฆฌํ๋ ์ฝ๋
System.out.println("์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค!");
}
System.out.println("ํ๋ก๊ทธ๋จ ์ข
๋ฃ");
}
}
package lecture0714;
public class Main {
public static void main(String[] args) {
System.out.println("ํ๋ก๊ทธ๋จ ์์!");
try { // exception์ด ๋ฐ์ํ ์ ์๋ ์ฝ๋
Object obj = null;
System.out.println(obj.toString());
} catch(ArithmeticException e) {
// exception์ ์ฒ๋ฆฌํ๋ ์ฝ๋
System.out.println("์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค!");
}
System.out.println("ํ๋ก๊ทธ๋จ ์ข
๋ฃ");
}
}
package lecture0714;
public class Main {
public static void main(String[] args) {
System.out.println("ํ๋ก๊ทธ๋จ ์์!");
try { // exception์ด ๋ฐ์ํ ์ ์๋ ์ฝ๋
Object obj = null;
System.out.println(obj.toString());
} catch(ArithmeticException e) {
// exception์ ์ฒ๋ฆฌํ๋ ์ฝ๋
System.out.println("์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค!");
} catch(NullPointerException e) {
System.out.println("์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค!");
}
System.out.println("ํ๋ก๊ทธ๋จ ์ข
๋ฃ");
}
}
package lecture0714;
public class Main {
public static void main(String[] args) {
System.out.println("ํ๋ก๊ทธ๋จ ์์!");
try { // exception์ด ๋ฐ์ํ ์ ์๋ ์ฝ๋
Object obj = null;
System.out.println(obj.toString());
int result = 10 / 0;
} catch(ArithmeticException e) {
// exception์ ์ฒ๋ฆฌํ๋ ์ฝ๋
System.out.println("ArithmeticException ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค!");
} catch(NullPointerException e) {
System.out.println("NullPointerException ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค!");
} finally {
// ๋ฌด์กฐ๊ฑด ์คํ๋๋ค.
System.out.println("finally ์คํ");
}
System.out.println("ํ๋ก๊ทธ๋จ ์ข
๋ฃ");
}
}
package lecture0714;
public class Main {
public static void main(String[] args) {
System.out.println("ํ๋ก๊ทธ๋จ ์์!");
try { // exception์ด ๋ฐ์ํ ์ ์๋ ์ฝ๋
Object obj = null;
System.out.println(obj.toString());
int result = 10 / 0;
} catch(Exception e) {
// exception์ ์ฒ๋ฆฌํ๋ ์ฝ๋
System.out.println("์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค!");
}
System.out.println("ํ๋ก๊ทธ๋จ ์ข
๋ฃ");
}
}
์์ Exception์ด catch๋ฌธ์ ๋จผ์ ๋ฑ์ฅํ๋ฉด ์๋๋ค.
public class Main {
public static void main(String[] args) {
System.out.println("ํ๋ก๊ทธ๋จ ์์!");
try { // exception์ด ๋ฐ์ํ ์ ์๋ ์ฝ๋
Object obj = null;
System.out.println(obj.toString());
int result = 10 / 0;
} catch(Exception e) {
// exception์ ์ฒ๋ฆฌํ๋ ์ฝ๋
System.out.println("์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค!");
} catch(NullPointerException e) { // ์ค๋ฅ ๋ฐ์
System.out.println("์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค!");
}
System.out.println("ํ๋ก๊ทธ๋จ ์ข
๋ฃ");
}
}