Tiny Bunny
๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป/JAVA

[JAVA] ์ž๋ฐ” ์˜ˆ์™ธ์ฒ˜๋ฆฌ(Exception)- try catch๋ฌธ, throw, throws

by soonybutter 2024. 9. 4.
728x90

 

 

 

 

 

 

ํ”„๋กœ๊ทธ๋žจ ์˜ค๋ฅ˜

 

  • ์ข…๋ฅ˜ 

1. ์ปดํŒŒ์ผ ์—๋Ÿฌ : ํ”„๋กœ๊ทธ๋žจ์˜ ์‹คํ–‰์„ ๋ง‰๋Š” ์†Œ์Šค ์ƒ ๋ฌธ๋ฒ• ์—๋Ÿฌ. ์†Œ์Šค ๊ตฌ๋ฌธ์„ ์ˆ˜์ •ํ•ด์„œ ํ•ด๊ฒฐ ํ•œ๋‹ค.

2. ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ : ์ž…๋ ฅ๊ฐ’์ด ์ด์ƒํ•˜๊ฑฐ๋‚˜, ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค ๋ฒ”์œ„ ์ดˆ๊ณผ ๋ฌธ์ œ, ๊ณ„์‚ฐ์‹์˜ ์˜ค๋ฅ˜๋“ฑ...

-> ์ฃผ๋กœ if๋ฌธ์œผ๋กœ ์—๋Ÿฌ๋ฅผ ์ฒ˜๋ฆฌํ•œ๋‹ค.

3. ์‹œ์Šคํ…œ ์—๋Ÿฌ: ์ปดํ“จํ„ฐ ์˜ค์ž‘๋™ ๋“ฑ..

 

 

 

  • ์—๋Ÿฌ(Exception) : ์†Œ์Šค ์ˆ˜์ •์œผ๋กœ ํ•ด๊ฒฐ ๊ฐ€๋Šฅํ•œ ์—๋Ÿฌ
  • ์šฐ๋ฆฌ๋Š” ์˜ˆ์ธก๊ฐ€๋Šฅํ•œ์—๋Ÿฌ(์˜ˆ์™ธ ์ƒํ™ฉ) ์„ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ†ตํ•ด ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 


 

 

 

์˜ˆ์™ธ์ฒ˜๋ฆฌ (Exception Handling)

 

  • ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ž€ ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•˜๋ฉฐ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ์˜ˆ์™ธ์˜ ์ƒํ™ฉ์— ๋Œ€๋น„ํ•œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์ฃผ๋Š” ์ฒ˜๋ฆฌ ๋ฐฉ์‹์ด๋‹ค.
  • ํ”„๋กœ๊ทธ๋žจ์˜ ๋น„์ •์ƒ์ ์ธ ๋™์ž‘์„ ๋ง‰์•„๋ฒ„๋ฆฌ๊ฑฐ๋‚˜, ๋ณต๊ตฌ๋ฅผ ์‹œ๋„ํ•˜๊ฑฐ๋‚˜(์—๋Ÿฌ ์žก๊ธฐ), ์ƒํ™ฉ์„ ํšŒํ”ผํ•˜๋Š” ๋ฐฉ์‹ ๋“ฑ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜์—ฌ ์ •์ƒ์ ์œผ๋กœ ํ”„๋กœ๊ทธ๋žจ์ด ์‹คํ–‰๋˜๋„๋ก ํ•œ๋‹ค.
  • ์ž๋ฐ”์—์„œ ๋‹ค๋ฃจ๋Š” ๋ชจ๋“  ์˜ˆ์™ธ ์˜ค๋ฅ˜๋Š” Exception ํด๋ž˜์Šค ์—์„œ ์ฒ˜๋ฆฌํ•œ๋‹ค.
  • ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ํด๋ž˜์Šค(Exception class)๋Š” ๋Ÿฐํƒ€์ž„์—๋Ÿฌ์™€ ์ž์‹ํด๋ž˜์Šค์—๋Ÿฌ(์ปดํŒŒ์ผ์—๋Ÿฌ)๋กœ ๋‚˜๋‰œ๋‹ค.

 

  • Exception ํด๋ž˜์Šค์™€ Errorํด๋ž˜์Šค ๋ชจ๋‘ Object ํด๋ž˜์Šค์˜ ์ž์†์ด๋‹ค.

๐ŸŒŸ

  • Checked Exception : ๋ฐ˜๋“œ์‹œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•˜๋Š” ์˜ค๋ฅ˜

          : IOException - EOFException, FileNotFoundException

  • Unchecked Exception : ์˜ˆ์™ธ์ฒ˜๋ฆฌ ํ•ด์ฃผ์ง€ ์•Š์•„๋„ ๊ดœ์ฐฎ์€ ์˜ค๋ฅ˜ ํฌํ•จ

 

 

+

[IOException] EOFException ํŒŒ์ผ์˜ ๋์— ๋„๋‹ฌํ–ˆ์„๋•Œ 

 

 

์‚ฌ์ง„์ถœ์ฒ˜- @Inpa Dev ํ‹ฐ์Šคํ† ๋ฆฌ

 

 

*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๋กœ ์ฒ˜๋ฆฌํ•˜์ž! 

(*์Šคํ”„๋ง ๋ฐฐ์šฐ๋ฉด ์ด ๋ถ€๋ถ„ ์ถ”๊ฐ€๊ณต๋ถ€ํ•  ์˜ˆ์ •..)

 

 

 

 

 

 


 

 

ํ‹€๋ฆฐ ๋ถ€๋ถ„์ด๋‚˜ ์ด์ƒํ•œ ๋ถ€๋ถ„์ด ์žˆ์œผ๋ฉด ๋Œ“๊ธ€๋กœ ์ง€์ ํ•ด์ฃผ์„ธ์š”! 

๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค :)

 

 

 

728x90

TOP

Designed by ํ‹ฐ์Šคํ† ๋ฆฌ