Item 75. Failure Information
์์ธ์ ์์ธ ๋ฉ์์ง์ ์คํจ ๊ด๋ จ ์ ๋ณด๋ฅผ ๋ด์ผ๋ผ
์์ธ๋ฅผ ์ก์ง ๋ชปํด ํ๋ก๊ทธ๋จ์ด ์คํจํ๋ฉด ์๋ฐ ์์คํ
์ ์คํ ์ถ์ (stack trace)์ ์ถ๋ ฅํ๋๋ฐ, ์คํ ์ถ์ ์ ์ถ๋ ฅ๋๋ ๋ฉ์์ง๋ ์์ธ ๊ฐ์ฒด์ toString
๋ฉ์๋๋ฅผ ํธ์ถํด ์ป๋ ๋ฌธ์์ด์ด๋ค.
๋๋ฌธ์ toString
๋ฉ์๋์ ๊ฐ๋ฅํ ๋ง์ ์ ๋ณด๋ฅผ ๋ด์ ๋ฐํํ๋ ๊ฒ์ด ๋๋ฒ๊น
์ ๋์์ด ๋๋ค.
์คํจ ๋ฉ์์ง์ ๋ด์ ์ ๋ณด
์คํจ ์๊ฐ์ ์ ํํ ํฌ์ฐฉํ๊ธฐ ์ํด์ ์์ธ์ ๊ด์ฌ๋ ๋ชจ๋ ๋งค๊ฐ๋ณ์์ ํ๋์ ๊ฐ์ ์คํจ ๋ฉ์์ง์ ๋ด์์ผ ํ๋ค.
์๋ฅผ๋ค์ด IndexOutOfBoundsException
์, ๋ฒ์์ ์ต์๊ฐ๊ณผ ์ต๋๊ฐ, ๊ทธ๋ฆฌ๊ณ ์ธ๋ฑ์ค์ ์ค์ ๊ฐ์ ๋ด์ผ๋ฉด ์ข์ง๋ง, ์๋ฐ์์๋(17 ๋ฒ์ ๊ธฐ์ค) ๋ฌธ์์ด์ด๋ ์ ์ ์ธ๋ฑ์ค ๊ฐ์ ๋ฐ๋ ์์ฑ์๋ง ์กด์ฌํ๋ค.
๋ง์ฝ ๋ ์์ธํ ์ ๋ณด๋ฅผ ์ ๊ณตํ๊ธฐ ์ํด์ ์๋์ ๊ฐ์ด ๋ฒ์์ ์ต์๊ฐ๊ณผ ์ต๋๊ฐ์ ๋ฐ๋ ์์ฑ์๊ฐ ์์ด๋ ์ข์์ ๊ฒ์ด๋ค.
class IndexOutOfBoundsException extends RuntimeException {
/**
* IndexOutOfBoundsException์ ์์ฑํ๋ค.
*
* @param lowerBound ์ธ๋ฑ์ค์ ์ต์๊ฐ
* @param upperBound ์ธ๋ฑ์ค์ ์ต๋๊ฐ + 1
* @param index ์ธ๋ฑ์ค์ ์ค์ ๊ฐ
*/
public IndexOutOfBoundsException(int lowerBound, int upperBound, int index) {
// ์คํจ๋ฅผ ํฌ์ฐฉํ๋ ์์ธ ๋ฉ์์ง๋ฅผ ์์ฑํ๋ค.
super(String.format(
"์ต์๊ฐ: %d, ์ต๋๊ฐ: %d, ์ธ๋ฑ์ค: %d",
lowerBound, upperBound, index));
// ํ๋ก๊ทธ๋จ์์ ์ด์ฉํ ์ ์๋๋ก ์คํจ ์ ๋ณด๋ฅผ ์ ์ฅํด๋๋ค.
this.lowerBound = lowerBound;
this.upperBound = upperBound;
this.index = index;
}
}
Last updated
Was this helpful?