
"์ค๋ ํ ๊ฐ์ง ๋ ์๊ธฐํ๊ณ ์ถ์์ต๋๋ค. ์๋ฐ์์ ๋ชจ๋ ์์ธ๋ ์ฒดํฌ( checked ) ์ ์ธ์ฒดํฌ (์ก์์ผ ํ๋ ๊ฒ๊ณผ ์ก์ง ์์๋ ๋๋ ๊ฒ)์ ๋ ๊ฐ์ง ์ ํ์ผ๋ก ๋๋ฉ๋๋ค. ๊ธฐ๋ณธ์ ์ผ๋ก ๋ชจ๋ ์์ธ ๋ ์ก์๋ค."
"์ฝ๋์์ ์๋์ ์ผ๋ก ์์ธ๋ฅผ ๋์ง ์ ์์ต๋๊น?"
"๋น์ ์ ๋น์ ์์ ์ ์ฝ๋์์ ์์ธ๋ฅผ ๋์ง ์ ์์ต๋๋ค. ๋น์ ์ ๋น์ ์์ ์ ์์ธ๋ฅผ ์์ฑํ ์๋ ์์ต๋๋ค. ๊ทธ๋ฌ๋ ๊ทธ๊ฒ์ ๋ํด์๋ ๋์ค์ ์ด์ผ๊ธฐํ ๊ฒ์ ๋๋ค. ์ง๊ธ ๋น์ฅ์ Java Machine์ด ๋์ง ์์ธ์ ์ง์คํฉ์๋ค."
"์ข์์."
" ๋ฉ์๋์์ ClassNotFoundException ๋๋ FileNotFoundException ์ด ๋ฐ์(๋ฐ์) ํ๋ ๊ฒฝ์ฐ ๊ฐ๋ฐ์๋ ๋ฉ์๋ ์ ์ธ์ ์ด๋ฅผ ํ์ํด์ผ ํฉ๋๋ค. ์ด๋ค์ ํ์ธ๋ ์์ธ์ ๋๋ค. ์ผ๋ฐ์ ์ผ๋ก ๋ค์๊ณผ ๊ฐ์ด ํ์๋ฉ๋๋ค."
ํ์ธ๋ ์์ธ์ ์ |
---|
|
|
|
"๊ทธ๋์ ์ฐ๋ฆฌ๋ 'throws' ๋ค์์ ์ผํ๋ก ๊ตฌ๋ถ๋ ์์ธ ๋ชฉ๋ก์ ์๋๋ค. ๋ง์ฃ ?"
"์. ํ์ง๋ง ๋ ์์ต๋๋ค. ํ๋ก๊ทธ๋จ์ ์ปดํ์ผํ๋ ค๋ฉด method1
์๋ ์์ ์์ ํธ์ถํ๋ ๋ฉ์๋๊ฐ ๋ ๊ฐ์ง ์์
์ค ํ๋๋ฅผ ์ํํด์ผ ํฉ๋๋ค. ์ด๋ฌํ ์์ธ๋ฅผ ํฌ์ฐฉํ๊ฑฐ๋ (ํธ์ถ์์๊ฒ) ๋ค์ throwํ์ฌ ์ ์ธ์์ ๋ค์ throw๋ ์์ธ๋ฅผ ๋ํ๋
๋๋ค. "
"๋ค์ ๋งํ์ง๋ง, ๊ธฐ๋ณธ ๋ฉ์๋๊ฐ ์ ์ธ์ ' throws FileNotFoundException , โฆ'๊ฐ ํฌํจ๋ ๋ฉ์๋๋ฅผ ํธ์ถํด์ผ ํ๋ ๊ฒฝ์ฐ ๋ค์ ๋ ๊ฐ์ง ์ค ํ๋๋ฅผ ์ํํด์ผ ํฉ๋๋ค.
1) FileNotFoundException ์บ์น , โฆ
์์ ํ์ง ์์ ๋ฉ์๋๋ฅผ ํธ์ถํ๋ ์ฝ๋๋ฅผ try-catch ๋ธ๋ก์ผ๋ก ๋ํํด์ผ ํฉ๋๋ค.
2) FileNotFoundException์ ์ก์ง ๋ง์ญ์์ค , โฆ
์ด๋ฌํ ์์ธ๋ฅผ ๊ธฐ๋ณธ ๋ฉ์๋ ์ throws ๋ชฉ๋ก ์ ์ถ๊ฐํด์ผ ํฉ๋๋ค ."
"์๋ฅผ ๋ค์ด ์ฃผ์๊ฒ ์ต๋๊น?"
"์ด๊ฒ ์ข ๋ณด์ธ์:"
public static void main(String[] args)
{
method1();
}
public static void method1() throws FileNotFoundException, ClassNotFoundException
{
//Throws FileNotFoundException if the file doesn't exist
FileInputStream fis = new FileInputStream("C2:\badFileName.txt");
}
"์ด ์์ ์ ์ฝ๋๋ ๋ฉ์ธ ๋ฉ์๋๊ฐ catchํด์ผ ํ๋ ์์ธ๋ฅผ throwํ๋ method1() ์ ํธ์ถ ํ๊ธฐ ๋๋ฌธ์ ์ปดํ์ผ๋์ง ์์ต๋๋ค ."
"์ปดํ์ผํ๋ ค๋ฉด ๊ธฐ๋ณธ ๋ฉ์๋์ ์์ธ ์ฒ๋ฆฌ๋ฅผ ์ถ๊ฐํด์ผ ํฉ๋๋ค. ๋ค์ ๋ ๊ฐ์ง ๋ฐฉ๋ฒ ์ค ํ๋๋ก ์ด๋ฅผ ์ํํ ์ ์์ต๋๋ค."
public static void main(String[] args) throws FileNotFoundException, ClassNotFoundException
{
method1();
}
public static void method1() throws FileNotFoundException, ClassNotFoundException
{
//Throws FileNotFoundException if the file doesn't exist
FileInputStream fis = new FileInputStream("C2:\badFileName.txt");
}
"์ฌ๊ธฐ์ try-catch ๋ก ์ก์ต๋๋ค ."
public static void main(String[] args)
{
try
{
method1();
}
catch(Exception e)
{
}
}
public static void method1() throws FileNotFoundException, ClassNotFoundException
{
//Throws FileNotFoundException if the file doesn't exist
FileInputStream fis = new FileInputStream("C2:\badFileName.txt");
}
"๋ช ํํด์ง๊ธฐ ์์ํ์ต๋๋ค."
"๋ค์ ์๋ฅผ ๋ณด์ญ์์ค. ๋๋จธ์ง๋ฅผ ์ดํดํ๋ ๋ฐ ๋์์ด ๋ ๊ฒ์ ๋๋ค."
public static void method2() throws FileNotFoundException, ClassNotFoundException
{
method1();
}
public static void method3() throws ClassNotFoundException
{
try
{
method1();
}
catch (FileNotFoundException e)
{
System.out.println("FileNotFoundException has been caught.");
}
}
public static void method4()
{
try
{
method1();
}
catch (FileNotFoundException e)
{
System.out.println("FileNotFoundException has been caught.");
}
catch (ClassNotFoundException e)
{
System.out.println("ClassNotFoundException has been caught.");
}
}
"์์ธ ์ ํ์ด ํ๋ ๋ ์๋๋ฐ RuntimeException ๊ณผ ์ด๋ฅผ ์์ํ๋ ํด๋์ค์ ๋๋ค. ์ด๋ฅผ ์ก์ ํ์๋ ์์ต๋๋ค. ์ด๋ค์ ํ์ธ๋์ง ์์ ์์ธ์ ๋๋ค. ์์ธกํ๊ธฐ ์ด๋ ค์ด ๊ฒ์ผ๋ก ๊ฐ์ฃผ๋ฉ๋๋ค. ๋์ผํ ๋ฐฉ์์ผ๋ก ์ฒ๋ฆฌํ ์ ์์ง๋ง throws ์ ์ ํ์ํ ํ์๊ฐ ์์ต๋๋ค ."
GO TO FULL VERSION