Try-catch, Exception
Error(์ค๋ฅ) ๐ Exception(์์ธ)
๋ ๋ค Throwable์ class
๐ฅ Error(์ค๋ฅ)
- ์ปดํจํฐ ์์ฒด์ ๋ฌธ์ (cpu, memory ๋ฑ)
- JVM์์ ๊ธฐ์ธํ ๋ฌธ์ โก๏ธ ์ฝ๋์ ๋ฌธ์ ๊ฐ ์๋
- ํต์ ๋ถ๊ฐ๋ฅ โ
- ํด๋์ค๋ก ๊ด๋ฆฌ๋๊ณ ์๋ค.
- example: OutOfMemoryError
๐ฅ Exception(์์ธ)
- ํต์ ๊ฐ๋ฅ โญ๏ธ ๐ฐ ์์ธ์ฒ๋ฆฌ
ํด๋์ค๋ก ๊ด๋ฆฌ๋๊ณ ์๋ค.
- Check Exception: ๊ผญ ์ฒ๋ฆฌํด์ผ ํ๋ ์์ธ, will not be coompiled
- IOException, SQL Exception
- ArrayBoundOutException ๋ฐฐ์ด 10๊ฐ์ธ๋ฐ 11๋ฒ์งธ ์ธ๋ฑ์ค ์ถ์ถ
- Uncheck Exception: ๋ฐ๋์ ์ฒ๋ฆฌํด์ผํ๋ ์์ธ๋ ์๋
- NullPointException
- RunTimeException
- NullPointerException
- IndexOutOfBoundException
- Check Exception: ๊ผญ ์ฒ๋ฆฌํด์ผ ํ๋ ์์ธ, will not be coompiled
- ํ์ค์ธ๊ณ์ ๋ง์ง ์์ ์์ฒญ(์ฌ๋ ํค ์์๊ฐ ๋ฃ๊ธฐ, 3๋ช ์ ํฐ์ผ๊ฐ ์ค 2๋ช ๋ง ๊ฒฐ์ )
- ์ธ๋ถ ํ๊ฒฝ ๋ณํ(์ธ๋ถ ์ธํ๋ผ ์ฌ๊ณ ๋ฐ์, ์ฝ๊ณ ์๋ ํ์ผ์ด ์ญ์ ๋๋ค๋๊ฐโฆ)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class ExceptionTest {
public static void main(String[] args) throws FileNotFoundException {
System.out.println("Main method run start");
//๐ฅ unCheckException
//ArrayBoundOutException
makeUncheckedException();
//๐ฅ checkedException
//๋นจ๊ฐ์ค ์ ๊ฑฐ ์ํค throws FileNotFoundException
makeCheckedException();
System.out.println("Main method end");
}
//๐ฅ unCheckException
public static void makeUncheckedException(){
int[] intArr= {1,2,3,4,5,6,7,8,9,10};
int index=11;
System.out.println(intArr[index]);
}
//๐ฅ checkedException
public static void makeCheckedException() throws FileNotFoundException {
File file= new File("src/chap50_errorexception/checkedExcpetion.txt");
FileInputStream fs= new FileInputStream(file);
//๋ฐ๋ก FileInputStream์ ๋นจ๊ฐ์ค
//๋นจ๊ฐ์ค ์์ ๊ธฐ ์ํด์๋ throws FileNotFoundException
//CheckedException์ ํ๋๋ก ์์ฒญ๋ฐ์ try-catch ํ๊ฑฐ๋ throw๋ฅผ ํด์ผ ํ๋ค.
}
}
๐ฅ Error(์ค๋ฅ)์ด๋ Exception(์์ธ) throw
๊ฐ์ฒด์ด๊ธฐ ๋๋ฌธ์ newํ์
ํจ์ ๋๊ฐ ๋ return โ throw โญ๏ธ
โ Try-catch
try: ๋ก์ง ์คํ catch: ์์ธ ๋ฐ์ํ๋ฉด ์คํ
try-catch ์ฌ์ฉ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class TryCatch1 {
public static void main(String[] args) {
System.out.println("Main method run start");
try {
int i = 0; //๋ง์ฝ i๊ฐ 50์ ์ฝ์์๋ค๋ฉด try๋ฌธ์ด ์คํ๋์์ ๊ฒ์ด๋ค.
int data = 50 / i; //๐ฅ exception ๋ฐ์
System.out.println("data"+ data);
} catch(ArithmeticException e){ // ๐ error๊ฐ ArithmeticException์ด๋ผ๋ฉด catch ์คํน
System.out.println("Cannot divide by 0");
System.out.println("data"+ 0);
e.printStackTrace(); //์ด๋ค error๋ฐ์ํ๋์ง ๋ด์ฉ ๋ณด๊ธฐ
}
System.out.println("Main method end");
}
}
โ Try-catch ๋ค์ค์ฒ๋ฆฌ
catch๋ฌธ์ด ์ฌ๋ฌ๊ฐ
try-catch ๋ ์ฌ๋ฌ๊ฐ ์ ์ธ๋ ๊ฐ๋ฅ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class TrCatch2 {
public static void main(String[] args) {
System.out.println("Main method running start");
List<String> stringList= new ArrayList<>();
stringList.add("Hello ");
stringList.add("World ");
stringList.add("I ");
stringList.add("Love ");
stringList.add(null); //๐ฅException ๋ฐ์, null์ ๋๋ฌธ์๋ก ๋ง๋ค์๊ฐ ์์์. ์ด ์ดํ๋ก stringList.add("Coding");๋ ์คํ์ด ์ ๋จ.
stringList.add("Coding");
for(int i=0; i<stringList.size()+5; i++){ //๐ฅException ๋ฐ์, arraysize๋ณด๋ค ๋ ๋ง์ด ์ถ๋ ฅํ๋ ค๊ณ ํจ
try{
String str= stringList.get(i);
System.out.println(str.toUpperCase());
} catch(NullPointerException e){ //๐
System.out.println("Cannot change null to uppercase.");
} catch(IndexOutOfBoundsException e){ //๐
System.out.println("Index cannot be over arraysize.");
break;
}
}
System.out.println("Main method end");
}
}
โ๏ธ ์ฒซ ๋ฒ์งธ exception ๋ฐ์ ์ดํ๋ก๋ ์งํํ์ง ์๋๋ค.
1
2
3
4
5
6
7
8
9
//๐ฅ ๋๋๊ธฐ Arithmatic exception
//โญ๏ธ ์ฒซ ๋ฒ์งธ exception ๋ฐ์ ์ดํ๋ก๋ ์งํํ์ง ์๋๋ค.
int i=0;
int data= 50/i;
//โญ์๋ ์คํ ์ ๋จ
//๐ฅString null
String str= null;
System.out.println(str.toUpperCase());
โ๏ธ try catch ๋ค์ค OR๋ก ์ ์ธ |
๋ ์ค๋ฅ ์ค ํ๋ ๋๋ฌธ์ ์ค๋ฅ๋์ catch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class TryMultiCatch {
public static void main(String[] args) {
try{
//
//์ด๋ ๊ฒ ํ๋ํ๋ Exception์ ์ํด๋ ๋์ง๋ง
// }catch(FileNotFoundException e){
// System.out.println("FileNotFoundException");
// e.printStackTrace();
// }catch(ArithmeticException e){
// System.out.println("ArithmeticException");
// }catch(NullPointerException e){
// System.out.println("NullPointerException");
// }
}catch(FileNotFoundException e){ //๐
System.out.println("FileNotFoundException");
e.printStackTrace();
//OR ์ฌ์ฉํด์ Exception
}catch(ArithmeticException | NullPointerException e ){ //๐
System.out.println("ArithmeticException or NullPointerException");
}
}
}
โ๏ธ ํ์ผ์ ์ฝ์ด์ฌ ๋๋ ๊ผญ exception ์์ธ์ฒ๋ฆฌ๋ฅผ ํด ์ฃผ์ด์ผ ํ๋ค.
throw๋๋ try-catch ์ฌ์ฉ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class TryCatch3 {
public static void main(String[] args) {
try {
FileInputStream fs = new FileInputStream("src/chap50_errorexception/test.txt"); //๐ฅException ๋ฐ์
int i;
while((i=fs.read()) != -1){ //๐ฅException ๋ฐ์, file์ฝ์ด์ค๊ธฐ
System.out.write(i);
}
} catch(FileNotFoundException e){ //๐
System.out.println("File not found");
e.printStackTrace();
} catch(IOException e){ //๐
System.out.println("Problem while reading");
}
}
}
โ Exception e
๋ชจ๋ Exception๋ค์ ์ด Exception ์๋์ ์๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class TryMultiCatch {
public static void main(String[] args) {
try{
}catch(FileNotFoundException e){ //๐
System.out.println("FileNotFoundException");
e.printStackTrace();
}catch(ArithmeticException | NullPointerException e ){ //๐
System.out.println("ArithmeticException or NullPointerException");
}catch(Exception e){
//๐๋ชจ๋ exception์ catch
//์ด๋ค exception์ด ๋ฐ์ํ ์ง ์์์ด ๋์ง ์์ ๋, ์ด์ดํ ๊ฑฐ๋ฆ๋ง
//๋ชจ๋ exception์ ์ด exception์ class
//โ๏ธ ๋จ, ์์์ค๋ฉด ๋ชจ๋ ์ค๋ฅ ์ก์๋ฒ๋ ค ์คํ ์์ฒด๊ฐ ์๋จ
System.out.println("Exception class catch");
}
}
}
โ Try-catch finally
return์ด๋ ,
catch๋ก ์ก๋ ๋ชป ์ก๋ ,
(catchํ๋ , catch ๋ชปํ๋ )
try-catch ์์ญ ์ดํ ๋ฌด์กฐ๊ฑด ์คํ
๐ก finally ์ด์ : ๋ฆฌ์์ค๋ฅผ ๋ค์ ์ปดํจํฐ์๊ฒ ๋๋ ค์ฃผ์ด์ผ ํ๊ธฐ ๋๋ฌธ์ด๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class FinallyCloseTest {
public static void main(String[] args) throws IOException {
System.out.println("Main method run start");
FileInputStream fs= null;
try {
//๐๏ธ ๋ฆฌ์์ค ๋ถ๋ฌ์ด
fs = new FileInputStream("src/chap51_trycatch/test.txt");
int i;
while((i=fs.read()) != -1){
System.out.write(i);
}
int myInt=10;
int data= 100/myInt;
//fs.close(); //๐๏ธ ๋ฆฌ์์ค ํด์ ์ฝ๋
//๐ฅ fs๋ฅผ ๋ซ์์ผ์ง ๋ฆฌ์์ค๊ฐ ํด์ ๋๋๋ฐ, ๋ง์ฝ ArithmeticException์ด ๋ฐ์ํ๋ฉด ๋ฆฌ์์ค๋ฅผ ํด์ ํ ์ ์๋ ์ํฉ์ด ๋ฐ์ํ๋ค
} catch(FileNotFoundException e){
System.out.println("File not found");
e.printStackTrace();
} catch(IOException e){
System.out.println("Problem while reading");
}catch (ArithmeticException e){
System.out.println("Cannot be divided by 0");
}finally { //๋ฆฌ์์ค๋ ๋ฌด์กฐ๊ฑด ํด์ ํ๋๋ก finally
fs.close(); //๐๏ธ ๋ฆฌ์์ค ํด์ ์ฝ๋, ์ด๋ค ๊ฒฝ์ฐ์๋ ๋ฆฌ์์ค๋ ํด์ ํ๋๋ก
}
System.out.println("Main method end");
}
}
โ Try-with-resource, AutoCloseable ์ธํฐํ์ด์ค
๐ก Try-with-resource
finally์ฐ์ง ์๊ณ try์์ FileInputStream ๊ฐ์ ธ์ค๋ฉด close๊ฐ ์๋์ผ๋ก ์คํ๋จ
Try-with-resource๋ AutoCloseable์ ์์ํ class๋ง ์ฌ์ฉ ๊ฐ๋ฅํ๋ค
์์ finally์ฝ๋๋ฅผ Try-with-resource๋ฅผ ์ฌ์ฉํด์ ์์
- Try-with-resource์์ด์ closeํ์ ์์
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class FinallyCloseTest {
public static void main(String[] args) throws IOException {
System.out.println("Main method run start");
//๐ก Try-with-resource
//closeํ๊ณ ์ถ์ ๋ฆฌ์์ค๋ฅผ try()์์ ๋ฃ๋๋ค.
try (FileInputStream fs = new FileInputStream("src/chap51_trycatch/test.txt")){
int i;
while((i=fs.read()) != -1){
System.out.write(i);
}
int myInt=10;
int data= 100/myInt;
} catch(FileNotFoundException e){
System.out.println("File not found");
e.printStackTrace();
} catch(IOException e){
System.out.println("Problem while reading");
}catch (ArithmeticException e){
System.out.println("Cannot be divided by 0");
}
System.out.println("Main method end");
}
}
โ ์์ธ ์ฒ๋ฆฌ ๋ฏธ๋ฃจ๊ธฐ: throw
๊ฐ ํจ์์์ try, catch๋ก ์์ธ ์ฒ๋ฆฌํ์ง ์๊ณ mainํจ์๋ก throwํด์ ๋ฏธ๋ค๋ฒ๋ฆฐ๋ค.
โ๏ธ๋จ, mainํจ์๋ ๊ฐ์ฅ ์ต์ข
์ผ๋ก ์คํ๋๋ ํจ์๋๊น throwํ ์ ์๋ค.
๋ง์ฝ mainํจ์๋ throwํด๋ฒ๋ฆฌ๋ฉด ์์ธ ์ฒ๋ฆฌ ์ ํ๋ ์
์ด ๋๋ค.
๋ฐ์ํ ๊ณณ์์ ์ฒ๋ฆฌํ์ง ์๊ณ , ๋๋ฅผ ํธ์ถํ ๋ค๋ฅธ ํจ์์๊ฒ throwํด์ ์์ํ๊ธฐ
๋ฐ์ ์ด์ :์ฌ๋ฌ ๊ฐ์ ํจ์์์ ๋น์ทํ exception์ด ๋ฐ์ํ ๋ ๊ฐ์ ํจ์์์ ์ฒ๋ฆฌํ๋๊ฒ ์๋๋ผ,
main method์์ ์ด๋ฅผ ํ ๋ฒ์ ์ฒ๋ฆฌํ๊ธฐ
๊ฐ ํจ์์์ exception ์ฒ๋ฆฌํ๋ฉด ๋น์ทํ ์ฝ๋๊ฐ ๋ฐ๋ณต๋๋๊น.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class PushExceptionTest {
public static void main(String[] args) {
try {
printFile("src/chap51_trycatch/test.txt");
//์๊น printFile, getFileStream์์ ๋ฏธ๋ค๋(throw) ์์ธ์ฒ๋ฆฌ ๋ชจ๋ ์ฌ๊ธฐ์ ์คํ
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}
}
//๋ ํด๊ฒฐ์ mainํจ์์๊ฒ ๋ฏธ๋ฃจ๊ธฐ
//throws IOException
static void printFile(String fileName) throws IOException {
FileInputStream fs= getFileStream(fileName);
int i;
while((i=fs.read()) != -1){
System.out.write(i);
}
}
//ํ์ผ ๋ฏธ๋ฃจ๊ธฐ ํ๋ ํจ์
//์ํด๋ try, catch๋ก file๋ถ๋ฌ์ค๊ธฐ exceptionํด์ผ ํจ
//throws FileNotFoundException๋ก ํด๊ฒฐ์ printFileํจ์์๊ฒ ๋ฏธ๋ฃจ๊ธฐ
static FileInputStream getFileStream(String fileName) throws FileNotFoundException {
FileInputStream fs = new FileInputStream(fileName);
return fs;
}
}
โ๏ธ handle exceptions all at once
๋งค์ฐ ๋น์ทํ exception์ด ๋ฐ์ํ๋ ๋ ํจ์์์ ๊ฐ๊ฐ exception ์ฒ๋ฆฌํ๋๊ฒ ์๋๋ผ, main์์ ํ ๋ฒ์ ์ฒ๋ฆฌ โก๏ธ ๋ ํจ์จ์ ์ธ ์ฝ๋
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
////๐๐ป ๊ฑฐ์ ๋๊ฐ์ exception์ฒ๋ฆฌํ๋ ๋นํจ์จ์ ์ฝ๋
public class handleExceptionsAtOnce {
public static void main(String[] args) {
//txt file, csv file์ฝ์ด์ค๋ ํจ์
//๋ ํจ์ ๋ชจ๋ ๋น์ทํ๊ฒ exception์ฒ๋ฆฌํด ์ฃผ์ด์ผ ํ๋ค.
printTextFile("src/chap51_trycatch/test.txt");
printCSVFile("src/chap51_trycatch/test.csv");
}
//๊ฑฐ์ ๋๊ฐ์ exception์ฒ๋ฆฌ
public static void printTextFile(String fileName){
if(!fileName.contains(".txt")){
System.out.println("No Text File");
return;
}
try{
FileInputStream fs= new FileInputStream(fileName);
int i;
while((i= fs.read()) != -1){
System.out.write(i);
}
} catch(IOException e){
System.out.println("IOException");
}
}
//๊ฑฐ์ ๋๊ฐ์ exception์ฒ๋ฆฌ
public static void printCSVFile(String fileName){
if(!fileName.contains(".csv")){
System.out.println("No CSV File");
return;
}
try{
FileInputStream fs= new FileInputStream(fileName);
int i;
while((i= fs.read()) != -1){
System.out.write(i);
}
} catch(IOException e){
System.out.println("IOException");
}
}
}
๐ก ๋ ํจ์จ์ ์ธ ์ฝ๋๋ก ๋ณ๊ฒฝ!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//main์ผ๋ก throw
//main์์ ์ฒ๋ฆฌ
public class handleExceptionsAtOnce {
public static void main(String[] args) {
try {
printTextFile("src/chap51_trycatch/test.txt");
printCSVFile("src/chap51_trycatch/test.csv");
} catch(IOException e){
System.out.println("IOException");
}
}
//txt file, csv file์ฝ์ด์ค๋ ํจ์
//๋ ํจ์ ๋ชจ๋ ๋น์ทํ๊ฒ exception์ฒ๋ฆฌํด ์ฃผ์ด์ผ ํ๋ค.
public static void printTextFile(String fileName) throws IOException{
if(!fileName.contains(".txt")){
System.out.println("No Text File");
return;
}
FileInputStream fs= new FileInputStream(fileName);
int i;
while((i= fs.read()) != -1){
System.out.write(i);
}
}
public static void printCSVFile(String fileName) throws IOException{
if(!fileName.contains(".csv")){
System.out.println("No CSV File");
return;
}
FileInputStream fs= new FileInputStream(fileName);
int i;
while((i= fs.read()) != -1){
System.out.write(i);
}
}
}
โ ์ง์ ๋์ง๊ธฐ Check Exception
๋ฌธ์ ๋ฐ์ํ ์ ์๋ ํจ์์์ if๋ฌธ์ ์ฌ์ฉํด ๋ฌธ์ ์ํฉ์ ์ ์ํด๋๊ณ
mainํจ์๋ก exception์ throwํ๋ค.
๊ทธ๋ฌ๋ฉด ์ด ๋ฌธ์ ๋ฐ์ํ ์ ์๋ ํจ์๋ฅผ mainํจ์์์ ํธ์ถํ์ ๋ ๋ฌธ์ ๊ฐ ์
๊ธฐ๋ ์ํฉ์ด๋ฉด,
๋นจ๊ฐ์ค์ด ๋นก!
๊ทธ๋์ runํ๊ธฐ ์ ์ ๋ฌธ์ ๊ฐ ์๊ฒผ๋ค๋ ๊ฒ์ ์ ์๊ฐ ์๋ค.
exception๋ class์ ํ ์ข
๋ฅ์ด๊ณ , throwable์ ์์ํ๋ค.
if๋ฌธ๊ณผ ํจ๊ป ์์ธ ์ก๊ธฐ
์กฐ๊ฑด๋ฌธ์ผ๋ก ์ก์์, ์์ ํจ์๋ก throwํ๊ธฐ๋ ํ๋ค.
๋ฌธ์ ๊ฐ ์๊ธธ exception์ ๋ฏธ๋ฆฌ ์์ํ์ฌ if๋ฌธ์ผ๋ก ์ฒ๋ฆฌํด exception์ throwํ๋๋ก ํ๋ค.
๊ทธ๋ฌ๋ฉด main method๊ฐ ์ด ๋ฌธ์ ์๊ธธ ์ฌ์ง ์๋ method ๋ฌธ์ ์๊ฒ ๋ถ๋ฅด์์?
๊ทธ๋ฌ๋ฉด ๋นจ๊ฐ์ค์ด ๋ฑ!!! ์๊ธด๋ค!
๊ทธ๋ฌ๋ฉด main method๊ฐ ์ด exception์ ์ก์์ try, catch๋ก ์ฒ๋ฆฌํ๋ค.
๊ทธ๋ฌ๋ฉด runํ๊ธฐ ์ ์ ๋ฌธ์ ์๊ธด๋ค๋ ๊ฒ์ ์ ์ ์๋๊ฑฐ์ง~ โก๏ธ check Exception
๊ทธ๋ฌ๋ฉด ์๋ฌ ์์ด ์คํํ ์ ์์ง!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public class ThrowUncheckException {
public static void main(String[] args) {
System.out.println("main method start");
//โ
getIntElement
int result=0;
try {
result = getIntElement(100);
System.out.println("array result= " + result);
} catch(Exception e){
System.out.println("Exception throw of getIntElement is taken care of.");
e.printStackTrace();
}
//โ
getDivide
try {
result= getDivide(0);
System.out.println("divide result= " + result);
} catch(Exception e){
System.out.println("Exception throw of getDivide is taken care of.");
e.printStackTrace();
}
System.out.println("main method end");
}
//โ
getIntElement
static int getIntElement(int index) throws Exception{
int[] intArr= {1,2,3,4,5,6,7,8,9,10};
//์ฌ๊ธฐ์ ์ง์ exception ์์ํด์ if๋ฌธ์ผ๋ก ์ฒ๋ฆฌ
//๋ง์ฝ ๋ฐฐ์ด ๊ธธ์ด๋ณด๋ค ๋ ํฐ ๊ฐ์ ๋ฌ๋ผ๊ณ ํ๋ฉด ์ด๋กํ์ง?
//๋ฏธ๋ฆฌ exception์ฒ๋ฆฌํด๋์!
//ํจ์ ์ด๋ฆ ์์ throws Exception์ถ๊ฐ
//์ด๋ ๊ฒ ํ๋ฉด mainํจ์์์ getIntElement ์๋ชป ํธ์ถํ๋ฉด ๋นจ๊ฐ์ค ๋ฌ๋ค!
if(index>= intArr.length){
throw new Exception("this index does not exist on this array");
}
return intArr[index];
}
//โ
getDivide
static int getDivide(int num) throws Exception{
if(num == 0){
throw new Exception("cannot be divided with this number");
}
int data= 100/num;;
return data;
}
}
โ ์ฌ์ฉ์ ์ ์ ์์ธ ๋์ง๊ธฐ
์๋ฅผ ๋ค์ด, ์ฌ๋์ ๋์ด์ธ๋ฐ ์์๊ฐ ๋ค์ด์จ๋ค๋ฉด? ๋น์ฆ๋์ค์ ์๊ตฌ์ ๋ง๋ ์กฐ๊ฑด๋ค์ ๋์ง๋๋ก ์์ธ ์ฒ๋ฆฌํ๊ธฐ ๋ด ๋ง์๋๋ก ์กฐ๊ฑด ๋ง๋ค์ด๋๊ณ , ์ด ์กฐ๊ฑด์ ์ถฉ์กฑํ์ง ์์ผ๋ฉด Exception๋ฐ์ํ๋๋ก ํ๊ธฐ
์กฐ๊ฑด
- PTmemeber class
- ID๋ null์ด ์๋
- ID๋ 8์ ์ด์ 20์ ์ดํ
- height๋ ์์
- weight ์์
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//1๏ธโฃ PostiveNumException.java
public class PostitiveNumException extends RuntimeException{
public PostitiveNumException(String message) {
super(message);
}
}
//2๏ธโฃ IDFormatException.java
public class IDFormatException extends RuntimeException{
public IDFormatException(String message) {
super(message);
}
}
//3๏ธโฃ PTmember.java
//member์ ๋ํ class
//โญ๏ธ exception ํด๋์ค๋ค import
import chap51_trycatch.Exception.IDFormatException;
import chap51_trycatch.Exception.PostitiveNumException;
public class PTmember {
private String ID;
private String name;
private Integer height;
private Integer weight;
private String gender;
//โญ๏ธ ๋ชธ๋ฌด๊ฒ, ํค ์์์ฌ์ผ ๋๋ค๋ ์กฐ๊ฑด ์ ์
public PTmember(String name, Integer height, Integer weight, String gender) {
if(height<0 || weight<0){
throw new PostitiveNumException("Height or Weight cannot be less than 0");
}
this.name = name;
this.height = height;
this.weight = weight;
this.gender = gender;
}
//โญ๏ธ ID์กฐ๊ฑด ์ ์
public void setID(String ID) {
if(ID == null){
throw new IDFormatException("ID cannot be null");
}
if(ID.length() <4 || ID.length()>20){
throw new IDFormatException("ID cannot be shorter than 8 or longer than 20");
}
this.ID = ID;
}
@Override
public String toString() {
return "PTmember{" +
"ID='" + ID + '\'' +
", name='" + name + '\'' +
", height=" + height +
", weight=" + weight +
", gender='" + gender + '\'' +
'}';
}
}
//4๏ธโฃ Main.java
public class MemeberTest {
public static void main(String[] args) {
PTmember member1= new PTmember("Lee", 100, 100, "Female");
member1.setID("ABCDE");
System.out.println(member1);
//PTmember{ID='ABCDEFG', name='Lee', height=100, weight=100, gender='Female'}
PTmember member2= new PTmember("Jang", -100, -100, "Male");
member2.setID("ABCDEF");
System.out.println(member2);
//Height or Weight cannot be less than 0
PTmember member3= new PTmember("Kim", 100, 100, "Male");
member3.setID(null);
System.out.println(member3);
//ID cannot be null
PTmember member4= new PTmember("Park", 100, 100, "Male");
member4.setID("A");
System.out.println(member4);
//ID cannot be shorter than 8 or longer than 20
}
}