์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
- ์์คํธ๋ฆฌ
- ์ฝ๋ฆฐ์ด
- ์๋ฐ
- map()ํจ์
- branch ํฉ์น๊ธฐ
- ๋ด๋๋ด์ฐ
- java
- html
- React
- react_useeffect
- server๋น๊ต
- C++
- css
- ๋ฏธ๋์ด์ฟผ๋ฆฌ
- ์ปค๋ฐ๋ฉ์์ง ๋ณ๊ฒฝํ๊ธฐ
- ์ ๋ก์ท-์์ท-ํจ์ท
- node.js ์ด๊ธฐ์ค์
- ์น๊ฐ๋ฐ
- ์์์ปค๋ฐ ์๋ฐฉํฅ ์ฌ๋ฐฐ์น ์ค๋ฅ
- DB
- ์๊ณ ๋ฆฌ์ฆ
- JavaScript
- ์ฐจ๋ฑํ๋ผ์ด๋ฒ์
- react_usememo
- vercel
- react_usereducer
- props {}
- ์น๊ฐ๋ฐ๊ณต๋ถ
- react_usecallback
- js
- Today
- Total
Soony's House
[JAVA] ์๋ฐ ์์ธ์ฒ๋ฆฌ(Exception)- try catch๋ฌธ, throw, throws ๋ณธ๋ฌธ
[JAVA] ์๋ฐ ์์ธ์ฒ๋ฆฌ(Exception)- try catch๋ฌธ, throw, throws
soonybutter 2024. 9. 4. 20:45
ํ๋ก๊ทธ๋จ ์ค๋ฅ
- ์ข ๋ฅ
1. ์ปดํ์ผ ์๋ฌ : ํ๋ก๊ทธ๋จ์ ์คํ์ ๋ง๋ ์์ค ์ ๋ฌธ๋ฒ ์๋ฌ. ์์ค ๊ตฌ๋ฌธ์ ์์ ํด์ ํด๊ฒฐ ํ๋ค.
2. ๋ฐํ์ ์๋ฌ : ์ ๋ ฅ๊ฐ์ด ์ด์ํ๊ฑฐ๋, ๋ฐฐ์ด์ ์ธ๋ฑ์ค ๋ฒ์ ์ด๊ณผ ๋ฌธ์ , ๊ณ์ฐ์์ ์ค๋ฅ๋ฑ...
-> ์ฃผ๋ก if๋ฌธ์ผ๋ก ์๋ฌ๋ฅผ ์ฒ๋ฆฌํ๋ค.
3. ์์คํ ์๋ฌ: ์ปดํจํฐ ์ค์๋ ๋ฑ..
- ์๋ฌ(Exception) : ์์ค ์์ ์ผ๋ก ํด๊ฒฐ ๊ฐ๋ฅํ ์๋ฌ
- ์ฐ๋ฆฌ๋ ์์ธก๊ฐ๋ฅํ์๋ฌ(์์ธ ์ํฉ) ์ ์์ธ์ฒ๋ฆฌ๋ฅผ ํตํด ํด๊ฒฐํ ์ ์๋ค.
์์ธ์ฒ๋ฆฌ (Exception Handling)
- ์์ธ์ฒ๋ฆฌ๋ ํ๋ก๊ทธ๋จ์ ์คํํ๋ฉฐ ๋ฐ์ํ ์ ์๋ ์์ธ์ ์ํฉ์ ๋๋นํ ์ฝ๋๋ฅผ ์์ฑํด์ฃผ๋ ์ฒ๋ฆฌ ๋ฐฉ์์ด๋ค.
- ํ๋ก๊ทธ๋จ์ ๋น์ ์์ ์ธ ๋์์ ๋ง์๋ฒ๋ฆฌ๊ฑฐ๋, ๋ณต๊ตฌ๋ฅผ ์๋ํ๊ฑฐ๋(์๋ฌ ์ก๊ธฐ), ์ํฉ์ ํํผํ๋ ๋ฐฉ์ ๋ฑ์ผ๋ก ์ฒ๋ฆฌํ์ฌ ์ ์์ ์ผ๋ก ํ๋ก๊ทธ๋จ์ด ์คํ๋๋๋ก ํ๋ค.
- ์๋ฐ์์ ๋ค๋ฃจ๋ ๋ชจ๋ ์์ธ ์ค๋ฅ๋ Exception ํด๋์ค ์์ ์ฒ๋ฆฌํ๋ค.
- ์์ธ ์ฒ๋ฆฌ ํด๋์ค(Exception class)๋ ๋ฐํ์์๋ฌ์ ์์ํด๋์ค์๋ฌ(์ปดํ์ผ์๋ฌ)๋ก ๋๋๋ค.

- Exception ํด๋์ค์ Errorํด๋์ค ๋ชจ๋ Object ํด๋์ค์ ์์์ด๋ค.
๐
- Checked Exception : ๋ฐ๋์ ์์ธ ์ฒ๋ฆฌํด์ผ ํ๋ ์ค๋ฅ
: IOException - EOFException, FileNotFoundException
- Unchecked Exception : ์์ธ์ฒ๋ฆฌ ํด์ฃผ์ง ์์๋ ๊ด์ฐฎ์ ์ค๋ฅ ํฌํจ
+
[IOException] EOFException ํ์ผ์ ๋์ ๋๋ฌํ์๋

*Exception ํด๋์ค
1. Exception ๋ฐ ํ์ ํด๋์ค - ์ฌ์ฉ์์ ์ค์, ์ธ์ ์์ธ๋๋ฌธ์ ๋ฐ์ํ๋ ์ปดํ์ผ ์๋ฌ!
1-1. FileNotFoundException (์กด์ฌํ์ง ์๋ ํ์ผ ์ด๋ฆ์ ์ ๋ ฅ)
1-2. ClassNotFoundException (์ค์๋ก ํด๋์ค์ ์ด๋ฆ์ ์๋ชป ๊ธฐ์ฌ)
1-3. DataFormatException(์ ๋ ฅํ ๋ฐ์ดํฐ ํ์์ด ์๋ชป๋ ๊ฒฝ์ฐ)
-> ์ปดํ์ผ ์์ ์์ธ๊ฐ ๋ฐ์ํ๋ค.
2. RuntimeException ํด๋์ค - ํ๋ก๊ทธ๋๋จธ์ ์ค์๋ก ๋ฐ์ํ๋ ๋ฐํ์ ์์ธ!
2-1. IndexOutOfBoundsException (๋ฐฐ์ด์ด ๋ฒ์๋ฅผ ๋ฒ์ด๋จ)
2-2. NullPointerException (๊ฐ์ด null์ธ ์ฐธ์กฐ ๋ณ์์ ๋ฉค๋ฒ๋ฅผ ํธ์ถ)
2-3. ClassCastException (ํด๋์ค ๊ฐ์ ํ ๋ณํ์ ์๋ชปํจ)
2-4. ArithmeticException (์ ์๋ฅผ 0์ผ๋ก ๋๋ ์ฐ์ ์ค๋ฅ)
-> ๋ฐํ์ ์์ ๋ฐ์ํ๋ ์์ธ, ์ปดํ์ผ ์์ ์ฒ๋ฆฌํ ์ ์๋ค.
์์ผ๋ก ๋ค๋ฃฐ ์์ธ์ค๋ฅ๋ค์ ๋ชจ๋ ์์ Exception ํด๋์ค์์ ์ฒ๋ฆฌํ ๊ฒ์ด๋ค.
๊ทธ๋ผ ๋ค์ ์์ธ ์ฒ๋ฆฌ ๋ฐฉ๋ฒ์ผ๋ก ๋์๊ฐ๋ณด์.
์์ธ์ฒ๋ฆฌ ๋ฐฉ๋ฒ
์์ธ ์ฒ๋ฆฌ ๋ฐฉ๋ฒ์ 3๊ฐ์ง๋ฅผ ์ฌ์ฉํ๋ค.
1.throws
2.try-catch-finally๋ฌธ
3.throw
1. throws
-Exception ์ฒ๋ฆฌ๋ฅผ ํธ์ถํ ๋ฉ์๋์ ์์
- ์์ ์ ํธ์ถํ ๋ฉ์๋์ ์์ธ์ฒ๋ฆฌ์ ์ฑ ์์ ๋๊ธด๋ค!
- ๋ฉ์๋ ์ ์ธ ์ throws ExceptionName๋ฌธ์ ์ถ๊ฐํ๋ค.
- ํธ์ถํ ์์ ๋ฉ์๋์ ์ฒ๋ฆฌ๋ฅผ ์์ํ๋ ๋ฐฉ์
- ๊ณ์ ์์ํ๋ฉด ๊ทธ ๋์ main() ๋ฉ์๋์ด๋ค. ์ด๊ณณ์์๋ ์ฒ๋ฆฌ๋์ง์์ผ๋ฉด ๋น์ ์์ข ๋ฃ๋๋ค.
ex.
import java.io.IOException;
public class Example2 {
public static void main(String[] args) {
Example2 ex = new Example2();
try {
ex.methodA();
}
catch(IOException e) {
e.printStackTrace();
}
finally {
System.out.println("์ด์จ๊ฑฐ๋ ํ๋ก๊ทธ๋จ ์ข
๋ฃ!");
}
}
public void methodA() throws IOException {
methodB();
}
public void methodB() throws IOException {
methodC();
}
public void methodC() throws IOException {
throw new IOException(); //Exception ๋ฐ์
}
์์ ์์๋ throws๋ฅผ ํตํด methodC() -> methodB() -> methodA() -> main ๋ฌธ์ผ๋ก ๋์ ธ์ ธ์ try-catch๋ฌธ์ผ๋ก ์์ธ ์ฒ๋ฆฌ๋๋๊ฒ์ ํ์ธํ ์ ์๋ค.
2. try-catch- (finally)
-๋ค๋ฅธ์์ ํ๋ฆ์ผ๋ก ์ ๋ํ๊ธฐ
: try - catch - finally ๋ฌธ์ ์ด์ฉํด์ ์์ธ์ฒ๋ฆฌํ๋ค.
- try : ์์ธ๋ฐ์ํ ๊ฐ๋ฅ์ฑ ์๋ ์ฝ๋๋ฅผ ์์ ์ ๋๋ค.
- catch : try ๊ตฌ๋ฌธ์์ ์์ธ ๋ฐ์์์ ํด๋นํ๋ exception์ ๋ํ ์ฒ๋ฆฌ๋ฅผ ์ ๋๋ค.
์ฌ๋ฌ๊ฐ์ ์์ธ์ฒ๋ฆฌ๊ฐ ๊ฐ๋ฅํ๋ฐ, ์ด ๊ฒฝ์ฐ์ ์์ธ๊ฐ์ ์์๊ด๊ณ๋ฅผ ๊ณ ๋ คํด์ผํ๋ค. (๊ฐ catch๋ฌธ์ ์ ๋ ์์)
- finally : ์์ธ๋ฐ์ ์ฌ๋ถ์ ๊ด๊ณ์์ด ๊ผญ ์ฒ๋ฆฌํด์ผํ๋ ๋ก์ง์ ์ ๋๋ค. ์ค๊ฐ์ return์ ๋ง๋๋ finally ๊ตฌ๋ฌธ์ ์คํ๋๋ค. ๊ทธ๋ฌ๋ System.exit(); ์ ๋ง๋๋ฉด ํ๋ก๊ทธ๋จ์ด ์ข ๋ฃ๋๋ค.
try {
์์ธ๊ฐ ์๊ธธ ๊ฐ๋ฅ์ฑ์ด ์๋ ์ฝ๋ ์์ฑ
}
catch(์์ธ๋ฐ์ ํด๋์ค๋ช
e){
์์ธ์ฒ๋ฆฌ ์ฝ๋
}
finally{
์์ธ๋ฐ์๊ณผ ์๊ด์์ด ๋ฌด์กฐ๊ฑด ์งํ์ํฌ ์ฝ๋
}
ex.
//ArithmeticException(์ ์๋ฅผ 0์ผ๋ก ๋๋ ์ค๋ฅ) ์์ธ์ฒ๋ฆฌ
public void arithEx() {
int ran = 0;
double res = 0.0;
System.out.println("๋๋๋ ค๋ ์๋ฅผ ์ ์๋ก ์
๋ ฅ: ");
int inputNum = sc.nextInt();
ran = (int)(Math.random()*10)+1;
//์์ธ์ฒ๋ฆฌ!
try { //์์ธ ๋ฐ์ ๊ฐ๋ฅ์ฑ์ด ์๋ ์ฝ๋
int result = (ran/inputNum);
System.out.printf("&d / &d = &d", ran, inputNum,result);
}
catch(ArithmeticException e) { //์์ธ๋ฐ์์
System.out.println("0์ ์ ์ธํ๊ณ ์
๋ ฅํ์ธ์!!!!!");
e.printStackTrace();
System.out.println(e.getMessage());
}
finlly{
System.out.println("๊ทธ๋ฌ๋์ง ๋ง๋์ง ์คํํ์
.");
}
}
- ์๋ฌ๋ฉ์์ง ์ ๋ฆฌ
*e.printStackTrace() : ์๋ฌ์ ๋ฐ์ ๊ทผ์์ง๋ฅผ ์ฐพ์, ๋จ๊ณ๋ณ๋ก ์๋ฌ๋ฅผ ์ถ๋ ฅํ๋ค.
*e.getMessage() : ์๋ฌ์ ์์ธ์ ๊ฐ๋จํ๊ฒ ์ถ๋ ฅํ๋ค.
*e.toString() : ์๋ฌ์ Exception ๋ด์ฉ๊ณผ ์์ธ์ ์ถ๋ ฅํ๋ค.
3. throw
-๋ช ํํ ์๋ฏธ์ ์์ธ๋ก ๋ฐ๋ก ์ฒ๋ฆฌํ๊ธฐ, ๊ฐ๋ฐ์๋ค์ด ๋น์ฆ๋์ค ๋ก์ง์์ ์ฒ๋ฆฌํ๋ ๋ฐฉ์
- ๊ฐ๋ฐ์๊ฐ ์ง์ ์์ธ๋ฅผ ๋ฐ์์ํค๊ณ ์ถ์๋ ์ฐ๋ ๊ฒ.
- ์ฃผ๋ก RuntimeException(UnCheckedException) ์ฒ๋ฆฌ๋ฅผ ์ํด ์ฐ๋ ๋ฐฉ์์ด๋ค.
ํ์ง๋ง CheckedException (์ฒดํฌ์์ธ)๋ฅผ ์ฒ๋ฆฌํ ๋์๋ ์ฌ์ฉํ๋ค.
- Spring ํ๋ ์์ํฌ๋ฅผ ์ธ ๋ ExceptionHandler ์ ํจ๊ป ๋ง์ด ์ฐ์ธ๋ค.
public class ThrowTest {
public static void main(String[] args) {
int num1, num2;
num1 =10;
num2 =0;
try {
throwTest(num1,num2);
}
catch(ArithmeticException e){
e.printStackTrace();
System.out.println("ArithmeticException: "+ e.getMessage());
}
}
public static void throwTest(int a, int b) throws ArithmeticException{
throw new ArithmeticException();
}

์์ ์์ ์ฝ๋์์ throwTest() ์์ throw new ArithmeticException(); ์ ๋ฃ์ด์ฃผ์๋ค.
์ด๋ ๊ฒ ์ฌ์ฉ์๊ฐ ์ง์ ์์ธ๋ฅผ ๋ฐ์์์ผ์ฃผ๊ณ ์ถ์ ๋ถ๋ถ์ ์์ธ๋ฅผ ๋ฐ์์์ผ์ค ํ, ์ด๋ฅผ throws๋ก ๋์ง๋ค.
ํ์ง๋ง ๊ฒฐ๊ณผ์ null๊ฐ์ด ๋์จ๋ค...
main๋ฌธ์์ ์์ธ์ฒ๋ฆฌ๋ฅผ ํด์ค ๋ฉ์์ง ์ฒ๋ฆฌ๊ฐ null๋ก ๋์จ ๊ฒ์ด๋ค.
throw๋ Exception์ ๋์ ธ์ค๋ ์์ธ ๋ด์ฉ์ ๋์ ธ์ฃผ์ง ์๋๋ค.
๊ทธ๋์ ๊ฐ๋ฐ์๋ ์ง์ ์ด๋ ํ ์์ธ๋ฅผ ๋ฐ์์ํค๊ณ ์ถ์๋ throw๋ฅผ ํตํด Exception์ ์ฒ๋ฆฌํ๊ณ ,
์ด๋ Exception์ ๋ฐ๋ก ์ปค์คํฐ๋ง์ด์งํด์ ๋ง๋ค์ด ๊ทธ ์์ ๋ฉ์์ง๋ฅผ ๋ฃ์ด ๋์ ธ์ค๋ค.
๐์ ๋ฆฌ ๋ฐ ๊ฒฐ๋ก
1. CheckedException : ๊ผญ ์ฒ๋ฆฌํด์ค์ผํ๋ ์์ธ์ํฉ.
try-catch ๋ฌธ, throws ๋ก ์ฒ๋ฆฌํ์!
2. UncheckedException :
throw๋ก exception๋์ ธ์ฃผ๊ณ ExceptionHandler๋ก ์ฒ๋ฆฌํ์!
(*์คํ๋ง ๋ฐฐ์ฐ๋ฉด ์ด ๋ถ๋ถ ์ถ๊ฐ๊ณต๋ถํ ์์ ..)
ํ๋ฆฐ ๋ถ๋ถ์ด๋ ์ด์ํ ๋ถ๋ถ์ด ์์ผ๋ฉด ๋๊ธ๋ก ์ง์ ํด์ฃผ์ธ์!
๊ฐ์ฌํฉ๋๋ค :)