Java

[Effective Java] ์•„์ดํ…œ 75. ์˜ˆ์™ธ์˜ ์ƒ์„ธ ๋ฉ”์‹œ์ง€์— ์‹คํŒจ ๊ด€๋ จ ์ •๋ณด๋ฅผ ๋‹ด์œผ๋ผ.

quedevel 2023. 4. 5. 11:00
728x90
๋ฐ˜์‘ํ˜•

๐ŸŽฏ ์•„์ดํ…œ 75. ์˜ˆ์™ธ์˜ ์ƒ์„ธ ๋ฉ”์‹œ์ง€์— ์‹คํŒจ ๊ด€๋ จ ์ •๋ณด๋ฅผ ๋‹ด์œผ๋ผ.

@Override
public Object invoke(Object target, Object... parameters) {
    try {
        return ( (Method) getMember() ).invoke( target, parameters );
    }
    catch (NullPointerException e) {
        throw new IllegalArgumentException( "Invoking " + getName() + " on a  null object", e );
    }
    catch (IllegalArgumentException e) {
        throw new IllegalArgumentException( "Invoking " + getName() + " with wrong parameters", e );
    }
    catch (Exception e) {
        throw new IllegalStateException( "Unable to invoke " + getName(), e );
    }
}

์‚ฌํ›„ ๋ถ„์„์„ ์œ„ํ•ด ์‹คํŒจ ์ˆœ๊ฐ„์˜ ์ƒํ™ฉ์„ ์ •ํ™•ํžˆ ํฌ์ฐฉํ•ด ์˜ˆ์™ธ์˜ ์ƒ์„ธ ๋ฉ”์‹œ์ง€์— ๋‹ด์•„์•ผ ํ•œ๋‹ค. ์‹คํŒจ ์ˆœ๊ฐ„์„ ํฌ์ฐฉํ•˜๋ ค๋ฉด ๋ฐœ์ƒํ•œ ์˜ˆ์™ธ์— ๊ด€์—ฌ๋œ ๋ชจ๋“  ๋งค๊ฐœ๋ณ€์ˆ˜์™€ ํ•„๋“œ์˜ ๊ฐ’์„ ์‹คํŒจ ๋ฉ”์‹œ์ง€์— ๋‹ด์•„์•ผ ํ•œ๋‹ค.


์˜ˆ์™ธ๋Š” ์‹คํŒจ ๊ด€๋ จ ์ •๋ณด๋ฅผ ์ ‘๊ทผ์ž ๋ฉ”์„œ๋“œ๋ฅผ ์ ์ ˆํžˆ ์ œ๊ณตํ•˜๋Š”๊ฒŒ ์ข‹๋‹ค.

์˜ˆ์™ธ์˜ ๋ชฉ์ ์„ ๋‹ค์‹œ ๋งํ•˜์ž๋ฉด ๊ฐœ๋ฐœ์ž์—๊ฒŒ ํฌ์ฐฉํ•œ ์‹คํŒจ ์ •๋ณด๋ฅผ ์ž˜ ์ „๋‹ฌํ•ด ์˜ˆ์™ธ ์ƒํ™ฉ์„ ๋ณต๊ตฌํ•˜๋Š”๋ฐ ์žˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— ์ ์ ˆํ•œ ์ ‘๊ทผ์ž ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด ์˜ˆ์™ธ ๋‚ด์šฉ์„ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด ๋”์šฑ ์ข‹๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋Ÿฐ ์‹คํŒจ ์ •๋ณด๋Š” ๋น„๊ฒ€์‚ฌ ์˜ˆ์™ธ(Unchecked Exception)๋ณด๋‹ค๋Š” ๊ฒ€์‚ฌ ์˜ˆ์™ธ(Checked Exception)์—์„œ ๋”์šฑ์ด ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ๋  ๊ฒƒ์ด๋‹ค.


์ฐธ๊ณ  ์ž๋ฃŒ

Joshua Bloch, ใ€ŽEffective Java 3/Eใ€, ๊ฐœ์•ž๋งต์‹œ ์˜ฎ๊น€, ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ธ์‚ฌ์ดํŠธ(2018)
http://www.kyobobook.co.kr/product/detailViewKor.laf?ejkGb=KOR&mallGb=KOR&barcode=9788966262281&orderClick=LEa&Kc=

728x90
๋ฐ˜์‘ํ˜•