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("ํ์ผ์์ด์!");
}
}
}