ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • JAVA ์ž๋ฐ” ์˜ˆ์™ธ์ฒ˜๋ฆฌ, throws, ์‚ฌ์šฉ์ž์ •์˜ ์˜ˆ์™ธํด๋ž˜์Šค
    JAVA 2020. 8. 10. 23:55

    ๐ŸŽฏ ์ž๋ฐ” ์˜ˆ์™ธ์ฒ˜๋ฆฌ์™€ throwsํ‚ค์›Œ๋“œ, ์‚ฌ์šฉ์ž์ •์˜ ์˜ˆ์™ธํด๋ž˜์Šค๋ฅผ ์•Œ์•„๋ณธ๋‹ค.


     

     

     

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

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

     

    ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋Š” ์˜ค๋ฅ˜๊ฐ€ ์žˆ๋Š” ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์•„๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์‹คํ–‰ํ•˜๋Š” ์ฝ”๋“œ๋Š” ์ˆœ์ฐจ๋ฌธ์ด๋ฉฐ, ํ•œ ๋ฒˆ ์‹คํ–‰๋œ ๋ฌธ์žฅ์€ ๋‹ค์‹œ ์‹คํ–‰์‹œํ‚ฌ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์˜ˆ์™ธ ๋ฐœ์ƒ์„ ์ˆ˜์ •ํ•˜๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. ๋”ฐ๋ผ์„œ ๋ฐœ์ƒํ•œ ์˜ค๋ฅ˜๊ฐ€ ํ”„๋กœ๊ทธ๋žจ ์ž‘๋™์— ๋ฐฉํ•ด๋˜์ง€ ์•Š๊ฒŒ ์˜ˆ์™ธ์ฒ˜๋ฆฌ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•œ๋‹ค.

     

    ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด ์ž๋ฐ”์—์„œ๋Š” ์˜ˆ์™ธํด๋ž˜์Šค API๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ์˜ˆ์™ธ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•œ Exceptionํด๋ž˜์Šค์˜ ํ•˜์œ„ํด๋ž˜์Šค์ธ Runtime Exception์—๋Š” ์šฐ๋ฆฌ๊ฐ€ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ArithmeticException, NullPointerException, ArrayIndexOutofBoundsException ๋“ฑ์˜ ํ•˜์œ„ํด๋ž˜์Šค๊ฐ€ ์žˆ๋‹ค.

     

     

    try ~ catch ๋ฌธ

    try{
    	๋ฌธ์žฅ1;
    	๋ฌธ์žฅ2;
    }catch( ์˜ˆ์™ธํด๋ž˜์Šค๋ช… e){
    	์˜ˆ์™ธ์ฒ˜๋ฆฌ์ฝ”๋“œ;
    }

    ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒ๋œ ์ฝ”๋“œ ๋‚ด์—์„œ ์˜ˆ์™ธ๋ฅผ ์ง์ ‘์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. try ๋ธ”๋ก ์•ˆ์— ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ๋ฅผ ์ ๊ณ , catch ๋ธ”๋ก ์•ˆ์— ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

    ์ฐธ์กฐ๋ณ€์ˆ˜ e๋Š” ์˜ˆ์™ธ๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ๋ฐœ์ƒ๋œ ์˜ˆ์™ธํด๋ž˜์Šค๋ž‘ ๋ณ€์ˆ˜ e์˜ ํด๋ž˜์Šคํƒ€์ž…์ด ๋™์ผํ•  ๊ฒฝ์šฐ ์ผ์น˜ํ•˜๋Š” catch๋ฌธ์ด ์‹คํ–‰๋œ๋‹ค. ์ฐธ์กฐ๋ณ€์ˆ˜ e๋ฅผ ํ†ตํ•ด ๋ฐœ์ƒ๋œ ์˜ˆ์™ธ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค.

     

    public class ArithmeticException {
    
    	public static void main(String[] args) {
    		System.out.println("ํ”„๋กœ๊ทธ๋žจ ์‹œ์ž‘");
    		int value = 0;
    		int num = 15/value;
    		int result = num + 100;
    		System.out.println("์—ฐ์‚ฐ๋œ ๊ฐ’: "+ result);
    		System.out.println("ํ”„๋กœ๊ทธ๋žจ ์ •์ƒ ์ข…๋ฃŒ");
    	}
    
    }

    15๋ฅผ 0์œผ๋กœ ๋‚˜๋ˆ„๋Š” ์ˆœ๊ฐ„ ์˜ˆ์™ธ ๋ฉ”์„ธ์ง€๊ฐ€ ์ถœ๋ ฅ๋˜๊ณ  ์‹คํ–‰์ด ๋น„์ •์ƒ์ ์œผ๋กœ ์ข…๋ฃŒ๋œ๋‹ค. ์‹คํ–‰๊ฒฐ๊ณผ๋Š” ํ”„๋กœ๊ทธ๋žจ ์‹œ์ž‘๊นŒ์ง€๋งŒ ์ถœ๋ ฅ๋œ๋‹ค. ์–ด๋–ค ์ˆ˜๋ฅผ 0์œผ๋กœ ๋‚˜๋ˆ„๋Š” ๊ฒฝ์šฐ ArithmeticException ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ์ด์ œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ†ตํ•ด ํ”„๋กœ๊ทธ๋žจ์ด ๋๊นŒ์ง€ ์ •์ƒ์ข…๋ฃŒ๋˜๋„๋ก ํ•ด์•ผํ•œ๋‹ค.

     

    public class Ex09_2 {
    	public static void main(String[] args) {
    		System.out.println("์‹œ์ž‘");
    		try {
    			int value = 0;
    			int num = 3 / value;
    			int result = num +100;
    			System.out.println("์—ฐ์‚ฐ๋œ ๊ฐ’: "+result);
    		}catch (ArithmeticException e) {
    		//	System.out.println(e.getMessage());
    			e.printStackTrace();
    		}
    		System.out.println("ํ”„๋กœ๊ทธ๋žจ ์ •์ƒ ์ข…๋ฃŒ");
    	}
    }

    3์„ 0์œผ๋กœ ๋‚˜๋ˆˆ ๋ถ€๋ถ„์—์„œ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค. ์ด ๋ถ€๋ถ„์„ try๋กœ ๋ฌถ์–ด์ค€๋‹ค. ์‹œ์Šคํ…œ์€ ์ด ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ์ ์ ˆํ•œ ์˜ˆ์™ธํด๋ž˜์Šค๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด ์ž๋™์œผ๋กœ ๊ฐ์ฒด์ƒ์„ฑ์„ ํ•˜๊ณ , Exception e = new Exception("์˜ˆ์™ธ์ถœ๋ ฅ๋ฌธ์ž์—ด"); ์˜ˆ์™ธ์ฒ˜๋ฆฌ์‹œ ํ•„์š”ํ•œ ๋ชจ๋“  ์˜ˆ์™ธ์ •๋ณด๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๋ณ€์ˆ˜ e๋ฅผ ์ž๋ฐ”์ฝ”๋“œ๋กœ ๋˜์ง„๋‹ค. ๋‚ด๋ถ€์ ์œผ๋กœ throw e๊ฐ€ ์‹คํ–‰๋˜์–ด catch์—์„œ ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ณ  ํ”„๋กœ๊ทธ๋žจ์ด ์ญ‰ ์‹คํ–‰๋˜๋ฉฐ ์ •์ƒ์ข…๋ฃŒ๋œ๋‹ค.

    ์‹œ์Šคํ…œ์—์„œ throwํ–ˆ๊ธฐ๋•Œ๋ฌธ์— ์ž๋ฐ”์ฝ”๋“œ์—์„œ๋Š” ๊ทธ๊ฑธ ์žก์•„์•ผ ํ•œ๋‹ค. ์˜ˆ์™ธํด๋ž˜์Šค ํƒ€์ž…์ด ์ผ์น˜ํ•ด์•ผ ์žก์„ ์ˆ˜ ์žˆ๊ณ , ๋‹คํ˜•์„ฑ์„ ์ ์šฉํ•˜๋ฉด ์ƒ์œ„ํด๋ž˜์Šค๋Š” ๋ชจ๋“  ํ•˜์œ„ ์˜ˆ์™ธํด๋ž˜์Šค๋ฅผ catchํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ๊ฒฝ์šฐ ์ตœ์ƒ์œ„ ์˜ˆ์™ธํด๋ž˜์Šค์ธ Exception์„ ์‚ฌ์šฉํ•œ๋‹ค.

     

    e.printStackTrace() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ–ˆ์„ ๊ฒฝ์šฐ ์˜ˆ์™ธ๋ฐœ์ƒ ๊ณผ์ •๊ณผ ์œ„์น˜๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.

     

    ์˜ˆ์™ธ์ฒ˜๋ฆฌ ์ฝ”๋“œ์—์„œ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•œ ๋ฉ”์†Œ๋“œ

    e.getMessage() : ๊ฐ„๋žตํ•˜๊ฒŒ ์˜ˆ์™ธ์ •๋ณด ์ถœ๋ ฅ

    e.printStackTrace() : ์˜ˆ์™ธ๋ฐœ์ƒ ๊ณผ์ •๊ณผ ์˜ˆ์™ธ ์ฝ”๋“œ ๋ผ์ธ๋„˜๋ฒ„ ์ถœ๋ ฅ

     

     

    ๋‹ค์ค‘ catch ๋ฌธ

    try{
    	๋ฌธ์žฅ1;
    	๋ฌธ์žฅ2;
    }catch( ์˜ˆ์™ธํด๋ž˜์Šค๋ช… ๋ณ€์ˆ˜๋ช…){
    	์˜ˆ์™ธ์ฒ˜๋ฆฌ์ฝ”๋“œ;
    }catch( ์˜ˆ์™ธํด๋ž˜์Šค๋ช… ๋ณ€์ˆ˜๋ช…2){
    	์˜ˆ์™ธ์ฒ˜๋ฆฌ์ฝ”๋“œ;
    }

    ์‹คํ–‰๋ฌธ์ด ์—ฌ๋Ÿฌ ๊ฐœ๋ฉด ๋‹น์—ฐํžˆ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ๋•Œ ๋ฐœ์ƒ๋œ ์˜ˆ์™ธ๋ฅผ ์ •ํ™•ํžˆ ๋ชจ๋ฅผ ๊ฒฝ์šฐ ์บ์น˜๋ฌธ์„ ๋‹ค์ค‘์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์ค‘ catch๋ฌธ์€ ์ƒ์„ฑ๋œ ์˜ˆ์™ธํด๋ž˜์Šค์™€ ๋ณ€์ˆ˜์˜ ๋ฐ์ดํ„ฐํ˜•์ด ๋‹ค๋ฅด๋ฉด ๋‹ค์Œ ์บ์น˜๋ฌธ์ด ์ˆ˜ํ–‰๋˜๋Š” ํ˜•ํƒœ์ด๋‹ค.

    ๋‹คํ˜•์„ฑ์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ๋ฐ˜๋“œ์‹œ ๊ณ„์ธต๊ตฌ์กฐ๊ฐ€ ๋‚ฎ์€ ํ•˜์œ„ํด๋ž˜์Šค๋ถ€ํ„ฐ catchํ•ด์•ผ ๋œ๋‹ค. ์ƒ์œ„ํด๋ž˜์Šค๋ถ€ํ„ฐ ์บ์น˜ํ•˜๋ฉด ์ฒซ๋ฒˆ์งธ ์บ์น˜๋ธ”๋ก์—์„œ ๋ชจ๋“  ์˜ˆ์™ธ๊ฐ€ ์ฒ˜๋ฆฌ๋˜์–ด ํ•˜์œ„ํด๋ž˜์Šค์˜ ๋ธ”๋ก์„ ์‹คํ–‰ํ•˜์ง€ ๋ชปํ•ด ์ปดํŒŒ์ผ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

     

    public class Ex09_3 {
    	public static void main(String[] args) {
    		System.out.println("ํ”„๋กœ๊ทธ๋žจ ์‹œ์ž‘");
    		String name = null;
    		try {
    			int num = 3 / 0;
    			System.out.println("๋ฌธ์ž์—ด์˜ ๊ธธ์ด:" + name.length());
    		}catch (NullPointerException e) {
    			System.out.println(e.getMessage());
    		}catch(ArithmeticException e2) {
    			System.out.println(e2.getMessage());
    		}catch(Exception e3) {
    			System.out.println("๋ชจ๋“  ์˜ˆ์™ธ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅ");
    		}
    		System.out.println("ํ”„๋กœ๊ทธ๋žจ ์ •์ƒ ์ข…๋ฃŒ");
    	}
    }

    try ๋ธ”๋ก์—์„œ 3์„ 0์œผ๋กœ ๋‚˜๋ˆ„์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ArithmeticException ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค. e์—์„  ์˜ˆ์™ธ๊ฐ€ ์žกํžˆ์ง€ ์•Š๊ณ , e2์—์„œ ์˜ˆ์™ธ๋ฅผ ์žก๊ณ  ์บ์น˜๋ฌธ์„ ์‹คํ–‰ํ•œ ํ›„ ํ”„๋กœ๊ทธ๋žจ์ด ์ •์ƒ ์ข…๋ฃŒ๋œ๋‹ค.

    NullPointerException์€ null๊ฐ’์„ ๊ฐ€์ง„ ์ฐธ์กฐ๋ณ€์ˆ˜๋กœ ๊ฐ์ฒด๋ฉค๋ฒ„ ์ ‘๊ทผ์‹œ ๋ฐœ์ƒํ•˜๋Š” ์˜ˆ์™ธ๋ฅผ ๋งํ•œ๋‹ค.

     

     

    finally ๋ฌธ

    try{
    	๋ฌธ์žฅ1;
    	๋ฌธ์žฅ2;
    }catch( ์˜ˆ์™ธํด๋ž˜์Šค๋ช… ๋ณ€์ˆ˜๋ช…){
    	์˜ˆ์™ธ์ฒ˜๋ฆฌ์ฝ”๋“œ;
    }catch( ์˜ˆ์™ธํด๋ž˜์Šค๋ช… ๋ณ€์ˆ˜๋ช…2){
    	์˜ˆ์™ธ์ฒ˜๋ฆฌ์ฝ”๋“œ;
    }finally{
    	๋ฐ˜๋“œ์‹œ ์ˆ˜ํ–‰๋˜๋Š” ๋ฌธ์žฅ
    }

    ์˜ˆ์™ธ ๋ฐœ์ƒ ์—ฌ๋ถ€์™€ ์ƒ๊ด€์—†์ด ํ•ญ์ƒ ์‹คํ–‰๋˜์–ด์•ผ ํ•˜๋Š” ๋ฌธ์žฅ์„ ์ง€์ •ํ•œ๋‹ค. 

     

     

    throws - ์˜ˆ์™ธ์ฒ˜๋ฆฌ ์œ„์ž„

    [์ง€์ •์ž] ๋ฆฌํ„ดํƒ€์ž… ๋ฉ”์†Œ๋“œ๋ช… ([ํŒŒ๋ผ๋ฏธํ„ฐ]) throws ์˜ˆ์™ธํด๋ž˜์Šค, ์˜ˆ์™ธํด๋ž˜์Šค2{ }

    ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ ๊ณณ์—์„œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ด์ง€๋งŒ ํ˜ธ์ถœํ•œ ๊ณณ์œผ๋กœ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ๋„˜๊ธฐ๋Š” ๊ฒƒ์ด throws ๋ฐฉ๋ฒ•์ด๋‹ค. ๊ณ„์† ์œ„์ž„ํ•˜๋ฉด mainํ•จ์ˆ˜๊นŒ์ง€ ๊ฐ€๊ฒŒ๋˜๊ณ  ์ตœ์ข…์ ์œผ๋กœ ๋ฉ”์ธํ•จ์ˆ˜์—์„œ try catch๋กœ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค. ๋ฉ”์ธ์—์„œ๋„ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ ํ”„๋กœ๊ทธ๋žจ์€ ๋น„์ •์ƒ ์ข…๋ฃŒ๋œ๋‹ค.

     

    public class Ex09_5 {
    	
    	public static void a() throws ArithmeticException{
    		b();
    	}
    	
    	public static void b() throws ArithmeticException {
    		int num = 3 /0;
    	}
    	
    	public static void main(String[] args) {
    		System.out.println("ํ”„๋กœ๊ทธ๋žจ ์‹œ์ž‘");
    		try {
    			a();
    		}catch(ArithmeticException e) {
    			System.out.println(e.getMessage());
    		}
    		System.out.println("ํ”„๋กœ๊ทธ๋žจ ์ •์ƒ ์ข…๋ฃŒ");
    	}
    
    }

    ๋ฉ”์ธ๋ฉ”์†Œ๋“œ์—์„œ ํ”„๋กœ๊ทธ๋žจ์ด ์‹œ์ž‘๋˜๊ณ  try ๋ธ”๋ก์—์„œ ์˜ˆ์™ธ๊ฐ€ ์žˆ๋Š” a๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. a๋ฉ”์†Œ๋“œ๋ฅผ ๊ฐ€๋ณด๋‹ˆ b๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๊ธธ๋ž˜ b๋ฉ”์†Œ๋“œ๋ฅผ ์‹คํ–‰ํ•ด๋ณด๋‹ˆ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค. throws๋Š” ์ด ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ˜ธ์ถœํ•œ ๊ณณ์œผ๋กœ ๋– ๋„˜๊ธฐ๋Š” ํ‚ค์›Œ๋“œ๋‹ค. b๋ฅผ ํ˜ธ์ถœํ•œ ๊ณณ a๋ฅผ ๊ฐ€๋ณด๋‹ˆ ๋˜ throws ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ˜ธ์ถœํ•œ main๋ฉ”์†Œ๋“œ๋กœ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ๋„˜๊ธด๋‹ค. ๋”ฐ๋ผ์„œ ๋ฉ”์ธ๋ฉ”์†Œ๋“œ ์•ˆ์—์„œ ๋„˜๊ฒจ์ง„ ์˜ˆ์™ธ๋ฅผ ์žก๊ณ  ์ฒ˜๋ฆฌํ•ด์•ผํ•œ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ํ”„๋กœ๊ทธ๋žจ์€ ์ •์ƒ์ข…๋ฃŒ๋œ๋‹ค.

     

    public class Test {
    	public static void a() throws NullPointerException {
    		b();
    	}
    
    	public static void b() throws NullPointerException {
    		String name = null;
    		System.out.println(name.length());
    	}
    
    	public static void main(String[] args) {
    		System.out.println("ํ”„๋กœ๊ทธ๋žจ ์‹œ์ž‘");
    		try {
    			a();
    		}catch(NullPointerException e) {
    			System.out.println(e.getMessage());
    		}
    		System.out.println("ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ");
    	}
    	
    }

    ๋ฉ”์ธ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ aํ•จ์ˆ˜ ํ˜ธ์ถœ -> aํ•จ์ˆ˜๋Š” bํ•จ์ˆ˜ ํ˜ธ์ถœ -> bํ•จ์ˆ˜๋Š” NullPointerException ๋ฐœ์ƒ, ์ฒ˜๋ฆฌ๋Š” a์—๊ฒŒ ๋– ๋„˜๊น€ -> a๋Š” main์—๊ฒŒ ๋– ๋„˜๊น€ -> ์ตœ์ข… ๋„์ฐฉ์ง€์ธ main์—์„œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ ๋‹ด๋‹น -> ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ

     

     

    ์˜ค๋ฒ„๋ผ์ด๋”ฉ ๋ฉ”์†Œ๋“œ์—์„œ throws๋ฌธ์„ ์‚ฌ์šฉํ•œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ

    class SuperClass{
    	public void a() throws NullPointerException {}
    	public void b() throws Exception {}
    	public void c() throws ArithmeticException{}
    
    class SubClass extends SuperClass {
    	@Override
    	public void a() {}
    
    	@Override
    	public void b() throws RuntimeException {}
    
    	@Override
    	public void c() throws Exception {} //err
    }
    
    public class Ex09_6 {
    	public static void main(String[] args) {}
    }

    ๋ถ€๋ชจํด๋ž˜์Šค์ธ SuperClass์— 3๊ฐœ์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ๋งŒ๋“ค๊ณ  ๊ฐ๊ฐ ๋‹ค๋ฅด๊ฒŒ ์˜ˆ์™ธํด๋ž˜์Šค๋ฅผ throws ํ•œ๋‹ค. ๋ถ€๋ชจํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•˜๋Š” SubClass์— ๊ฐ๊ฐ์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉ ํ•œ๋‹ค. ์ด ๋•Œ throws ์˜ˆ์™ธํด๋ž˜์Šค๋Š” ๋ถ€๋ชจ์˜ ์˜ˆ์™ธํด๋ž˜์Šค๋ณด๋‹ค ๊ณ„์ธต๊ตฌ์กฐ๊ฐ€ ๋‚ฎ๊ฑฐ๋‚˜ ์•„์˜ˆ ์—†์–ด์•ผ ํ•œ๋‹ค. ์„œ๋ธŒํด๋ž˜์Šค์˜ c๋ฉ”์†Œ๋“œ๋Š” ๋ถ€๋ชจ์˜ ์˜ˆ์™ธํด๋ž˜์Šค๋ณด๋‹ค ์ƒ์œ„ํด๋ž˜์Šค์ธ Exception์„ ์‚ฌ์šฉํ•ด์„œ ์ปดํŒŒ์ผ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

     

     

    throw - ๋ช…์‹œ์  ์˜ˆ์™ธ ๋ฐœ์ƒ

    if( ์กฐ๊ฑด ){throw new ์˜ˆ์™ธํด๋ž˜์Šค(“์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฌธ์ž์—ด”);}

    ์˜ˆ์™ธํด๋ž˜์Šค์˜ ์กฐ๊ฑด์— ํ•ด๋‹น๋˜์ง€ ์•Š๋Š”๋ฐ ์˜ˆ์™ธ๋ฅผ ๋งŒ๋“ค๊ณ ์‹ถ์€ ๊ฒฝ์šฐ, ์กฐ๊ฑด์„ ์„ค์ •ํ•ด์„œ ๋ช…์‹œ์ ์œผ๋กœ ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ๋งŒ๋“  ํ›„ ๋ฐ˜๋“œ์‹œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ค˜์•ผ ๋น„์ •์ƒ ์ข…๋ฃŒ๋ฅผ ๋ง‰์„ ์ˆ˜ ์žˆ๋‹ค. ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” ์˜ˆ์™ธํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  throw๋กœ ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ ๋’ค throws๋กœ ๋„˜๊ฒจ์„œ ์˜ˆ์™ธ์ฒ˜๋ฆฌํ•ด์ฃผ๊ณ  ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•œ๋‹ค. 

    public class Ex09_5 {
    	
    	public static void a() throws ArithmeticException{
    		b();
    	}
    	
    	public static void b() throws NullPointerException {
    		int num = 10;
    		if(num == 10) {
    			//๋ช…์‹œ์  ๊ฐ•์ œ์ ์œผ๋กœ ํŠน์ • Exception ๋ฐœ์ƒํ•˜๋ฉด์„œ ๋ฉ”์„ธ์ง€ ์ง€์ •ํ•ด์ฃผ๊ธฐ
    			throw new NullPointerException("๋„ํฌ์ธํŠธ์ต์…‰์…˜๋ฐœ์ƒ");
    			//๊ฐ์ฒด์ƒ์„ฑํ•˜๋ฉด์„œ ์—๋Ÿฌ๋ฉ”์„ธ์ง€ ๋ณด์—ฌ์ฃผ๊ธฐ
    		}
    	}
    	
    	public static void main(String[] args) {
    		System.out.println("ํ”„๋กœ๊ทธ๋žจ ์‹œ์ž‘");
    		try {
    			a();
    		}catch(ArithmeticException e) {
    			System.out.println(e.getMessage());
    		}
    		System.out.println("ํ”„๋กœ๊ทธ๋žจ ์ •์ƒ ์ข…๋ฃŒ");
    	}
    
    }

    num์ด 10์ธ ๊ฒฝ์šฐ๋ฅผ ์˜ˆ์™ธ๋กœ ์ธ์‹ํ•˜๋Š” ์˜ˆ์™ธํด๋ž˜์Šค๋Š” ์—†๋‹ค. ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด throw๋ฅผ ์‚ฌ์šฉํ•ด ๋ช…์‹œ์  ๊ฐ•์ œ์ ์œผ๋กœ ์˜ˆ์™ธ๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. throw ํ‚ค์›Œ๋“œ ๋’ค์— new๋กœ ๊ฐ์ฒด์ƒ์„ฑ ํ•ด์ฃผ๊ณ  ๊ด„ํ˜ธ ์•ˆ์— ์˜ˆ์™ธ์ฒ˜๋ฆฌ ๋ฉ”์„ธ์ง€๋ฅผ ์ž‘์„ฑํ•˜๋ฉด ๋œ๋‹ค.

     

    public class ExTest {
    	public int a(int num1, int num2) throws ArithmeticException{
    		int result = 0;
    		result=num1/ num2;
    		return result;
    	}
    }
    public class Test {
    	public static void main(String[] args) {
    	 ExTest test= new ExTest();
    	 try {
    		 test.a(10, 0);
    	 }catch(Exception e) {
    		System.out.println(e.getMessage()); 
    	 }
    	}
    }

    ๋ฉ”์ธํ•จ์ˆ˜ ์•ˆ์—์„œ ExTest์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  a๋ฉ”์†Œ๋“œ์— ์ธ์ž๋ฅผ ์ ์–ด์„œ ํ˜ธ์ถœํ–ˆ๋‹ค. a๋ฉ”์†Œ๋“œ๋Š” ์ธ์ž๋ฅผ ๋ฐ›์•„์™€์„œ 10์„ 0์œผ๋กœ ๋‚˜๋ˆˆ๋‹ค. ์—ญ์‹œ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ์ด ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ˜ธ์ถœํ•œ ๊ณณ์œผ๋กœ ๋– ๋„˜๊ธฐ๊ณ  ์žˆ๋‹ค. ๋ฉ”์ธํ•จ์ˆ˜๋กœ ๋Œ์•„์™€ catch๋ฌธ์—์„œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ์™„๋ฃŒํ•œ๋‹ค.

    public class ExTest {
    	public int a(int num1, int num2) throws ArithmeticException{
    		int result = 0;
    		if(num2 == 0) {
    			throw new ArithmeticException("0์œผ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.");			
    		}else{
    			result = 0;
    			result=num1/ num2;
    		}
    		return result;
    	}
    }

    ์ด๋ฒˆ์—๋Š” ExTest์˜ a๋ฉ”์†Œ๋“œ์—์„œ throw ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ๋ช…์‹œ์ ์œผ๋กœ ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œ์ผฐ๋‹ค. ์ธ์ž๋กœ ๋ฐ›์€ num2๊ฐ€ 0์ด ์•„๋‹ˆ๋ผ ๋‹ค๋ฅธ ์ˆซ์ž์ผ ๊ฒฝ์šฐ์—” else๋ฌธ์ด ์‹คํ–‰๋˜์–ด ์ •์ƒ์ฒ˜๋ฆฌ๋œ๋‹ค. 

     

     

    ์‚ฌ์šฉ์ž ์ •์˜ ์˜ˆ์™ธํด๋ž˜์Šค

    ์‚ฌ์šฉ์ž๊ฐ€ ์ƒˆ๋กœ์šด ์˜ˆ์™ธํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ, Exception ์˜ˆ์™ธํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์•„ ์ž‘์„ฑํ•œ๋‹ค. 

    class UserException extends Exception {
    	public UserException(String mesg) {
    		super(mesg);
    	}
    }
    
    public class Ex09_8 {
    	public static void check(int num) throws UserException {
    		if (num < 100) {
    			throw new UserException("num ๊ฐ’์ด 100๋ณด๋‹ค ์ž‘๋‹ค");
    		}
    	}
    
    	public static void main(String[] args) {
    		System.out.println("ํ”„๋กœ๊ทธ๋žจ ์‹œ์ž‘");
    		try {
    			check(70);
    		} catch (UserException e) {
    			System.out.println("์˜ˆ์™ธ๋ฐœ์ƒ: " + e.getMessage());
    		}
    		System.out.println("ํ”„๋กœ๊ทธ๋žจ ์ •์ƒ ์ข…๋ฃŒ");
    	}
    
    }
    class ByZeroException extends Exception {
    	public ByZeroException(String mesg) {
    		super(mesg);
    	}
    }
    
    public class Ex09_9 {
    	public static void divide() throws ByZeroException{
    		try { 
    			int num = 3/0;
    		}catch (ArithmeticException e) {
    			throw new ByZeroException("0์œผ๋กœ ๋‚˜๋ˆ„์–ด ์˜ˆ์™ธ ๋ฐœ์ƒ~");
    		}
    	}
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		System.out.println("ํ”„๋กœ๊ทธ๋žจ ์‹œ์ž‘");
    		try {
    			divide();
    		}catch (ByZeroException e) {
    			System.out.println("์˜ˆ์™ธ๋ฐœ์ƒ: "+e.getMessage());
    		}
    		System.out.println("ํ”„๋กœ๊ทธ๋žจ ์ •์ƒ ์ข…๋ฃŒ");
    	}
    
    }

     

     

     

    ๐Ÿ˜œ NEXT : ์ž๋ฐ” ํ•ต์‹ฌํด๋ž˜์Šค ๊ณต๋ถ€ํ•˜๊ธฐ

    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

๊ฐœ๋ฐœ๊ณต๋ถ€