
"์ฌ๋ฏธ์๋ ์์ ์ด ๋ช ๊ฐ ๋ ์์ด์. ์ค, ๊ฐ๋ฅด์น๋ ๊ฒ ์ ๋ง ์ข์์!"
"์ฌ๋ฌ catch ๋ธ๋ก์ด ์๋ํ๋ ๋ฐฉ์์ ๋ํด ๋ง์๋๋ฆฌ๊ณ ์ถ์ต๋๋ค . ๋งค์ฐ ๊ฐ๋จํฉ๋๋ค. try ๋ธ๋ก์์ ์์ธ๊ฐ ๋ฐ์ํ๋ฉด ์คํ์ด ์ฒซ ๋ฒ์งธ catch ๋ธ๋ก์ผ๋ก ์ด๋ํฉ๋๋ค."
"catch ๋ธ๋ก์ ๊ดํธ ์์ ํ์๋ ์ ํ์ด ๋ฐ์ํ ์์ธ ์ ํ๊ณผ ์ผ์นํ๋ฉด ํด๋น ๋ธ๋ก ๋ด์์ ์คํ์ด ์์๋ฉ๋๋ค. ๊ทธ๋ ์ง ์์ผ๋ฉด ๋์ผํ ๊ฒ์ฌ๊ฐ ์ํ๋๋ ๋ค์ catch ๋ธ๋ก ์ผ๋ก ์ด๋ํฉ๋๋ค ."
" catch ๋ธ๋ก์ด ๋ถ์กฑ ํ๊ณ ์์ธ๊ฐ ํฌ์ฐฉ๋์ง ์์ผ๋ฉด ์์ธ๊ฐ ๋ค์ ๋ฐ์ํ๊ณ ํ์ฌ ๋ฉ์๋๊ฐ ๋น์ ์์ ์ผ๋ก ์ข ๋ฃ๋ฉ๋๋ค."
"๊ทธ๋ ๊ตฐ์. ์์ธ ์ ํ๊ณผ ์ผ์นํ๋ catch ๋ธ๋ก์ด ์คํ๋ ๊ฒ์ ๋๋ค."
"๋ค, ๋ง์ต๋๋ค. ํ์ง๋ง ์ค์ ๋ก๋ ์ข ๋ ๋ณต์กํฉ๋๋ค. ํด๋์ค๋ ๋ค๋ฅธ ํด๋์ค๋ฅผ ์์ํ ์ ์์ต๋๋ค. Cow ํด๋์ค๊ฐ Animal ํด๋์ค๋ฅผ ์์๋ฐ๋ ๊ฒฝ์ฐ Cow ๊ฐ์ฒด๋ Cow ๋ณ์๋ฟ๋ง ์๋๋ผ Animal ๋ณ์์ ์ํด์๋ ์ฐธ์กฐ๋ ์ ์์ต๋๋ค. "
"๊ทธ๋ฆฌ๊ณ ?"
"๋ชจ๋ ์์ธ๋ Exception ๋๋ RuntimeException ( Exception ๋ ์์ํจ )์ ์์ํ๊ธฐ ๋๋ฌธ์ ์ฌ์ ํ 'catch( Exception e)' ๋๋ ' catch(RuntimeException e) ' ๋ฅผ ์ฌ์ฉํ์ฌ ์ก์ ์ ์์ต๋๋ค ."
"๊ทธ๋ฆฌ๊ณ ?"
"์ฐ๋ฆฌ๋ ๋ ๊ฐ์ง ๊ฒฐ๋ก ์ ๋ด๋ฆด ์ ์์ต๋๋ค. ์ฒซ์งธ, 'catch(์์ธ e)'๋ฅผ ์ฌ์ฉํ์ฌ ๋ชจ๋ ์์ธ๋ฅผ ์ก์ ์ ์์ต๋๋ค. ๋์งธ, catch ๋ธ๋ก์ ์์๊ฐ ์ค์ํฉ๋๋ค. "
"์ฌ๊ธฐ ๋ช ๊ฐ์ง ์๊ฐ ์์ด์:"
" ArithmeticException
0์ผ๋ก ๋๋ ํ์ ๋ฐ์ํ๋ ๋ ๋ ๋ฒ์งธ catch ๋ธ๋ก์์ ์กํ๋๋ค."
try
{
System.out.println("Before calling method1.");
int a = 1 / 0;
System.out.println("After calling method1. This will never be shown.");
}
catch (NullPointerException e)
{
System.out.println("Null reference. Exception has been caught.");
}
catch (ArithmeticException e)
{
System.out.println("Division by zero. Exception has been caught.");
}
catch (Exception e)
{
System.out.println("Any other errors. Exception has been caught.");
}
"์๋ ์์์ ArithmeticException
๋ชจ๋ ์์ธ๋ ์์ธ๋ฅผ ์์ํ๋ฏ๋ก ์ฒซ ๋ฒ์งธ catch ๋ธ๋ก์์ catch๋ฉ๋๋ค. ์ฆ, Exception
๋ชจ๋ ์์ธ๋ฅผ ํฌํจํฉ๋๋ค. "
try
{
System.out.println("Before calling method1.");
int a = 1/0;
System.out.println("After calling method1. This will never be shown.");
}
catch (Exception e)
{
System.out.println("Any other errors. Exception has been caught.");
}
catch (NullPointerException e)
{
System.out.println("Null reference. Exception has been caught.");
}
catch (ArithmeticException e)
{
System.out.println("Divided by zero. Exception has been caught.");
}
"์๋ ์์์๋ ArithmeticException
์กํ์ง ์์ต๋๋ค. ํธ์ถ ๋ฉ์๋๋ก ๋ค์ throw๋ฉ๋๋ค."
try
{
System.out.println("Before calling method1.");
int a = 1/0;
System.out.println("After calling method1. This will never be shown.");
}
catch (NullPointerException e)
{
System.out.println("Null reference. Exception has been caught.");
}
"๊ทธ๊ฒ์ผ๋ก ๋ฌธ์ ๊ฐ ์กฐ๊ธ ์ ๋ฆฌ๋์์ต๋๋ค. ์ด๋ฌํ ์์ธ๋ ๊ฐ์ฅ ์ฌ์ด ์ฃผ์ ๊ฐ ์๋๋๋ค."
"๊ทธ๋ฅ ๊ทธ๋ฐ ๊ฒ ๊ฐ์ต๋๋ค. ์ค์ ๋ก Java์์ ๊ฐ์ฅ ๊ฐ๋จํ ๊ฒ ์ค ํ๋์ ๋๋ค."
"๊ธฐ๋ปํด์ผ ํ ์ง ์ฌํผํด์ผ ํ ์ง ๋ชจ๋ฅด๊ฒ ์ด..."
GO TO FULL VERSION