package ch09.unit01;
public class Ex15 {
public static void main(String[] args) {
Test15 t = new Test15();
// value๊ฐ 0๋ณด๋ค ์ ์ด์ RuntimeException > ๋น์ ์ ์ข
๋ฃ
/*
* t.setValue(-5); int n = t.getValue(); System.out.println(n);
*/
try {
t.setValue(-5);
int n = t.getValue();
System.out.println(n);
} catch (Exception e) {
System.out.println(e.toString());
}
System.out.println("end...");
}
}
class Test15 {
private int value;
public int getValue() {
return value;
}
public void setValue(int value) {
if (value < 0) {
// RuntimeException : unchecked ์์ธ์ ์์ ํด๋์ค
// ๋ฐ๋์ catch ํ ํ์๋ ์๋ค
// ์์ธ ์ฒ๋ฆฌ๋ฅผ ํ์ง ์์ ์ํ์์ ์์ธ๊ฐ ๋ฐ์ํ๋ฉด ๋น์ ์ ์ข
๋ฃ
throw new RuntimeException("0์ด์๋ง ๊ฐ๋ฅํฉ๋๋ค.");
}
this.value = value;
}
}
package ch09.unit01;
public class Ex16 {
public static void main(String[] args) {
Test16 t = new Test16();
try {
t.set("ํ๊ธธ๋", -5);
System.out.println(t.getNeme() + "," + t.getAge());
} catch (Exception e) {
System.out.println(e.toString());
}
System.out.println("end...");
}
}
class Test16 {
private String neme;
private int age;
public void set(String name, int age) throws Exception {
try {
setNeme(name);
setAge(age);
} catch (Exception e) {
//System.out.println(e.toString());
throw e;
//throw new Exception("์์ธ ๋ฐ์...");
}
}
public String getNeme() {
return neme;
}
public void setNeme(String neme) {
this.neme = neme;
}
public int getAge() {
return age;
}
public void setAge(int age) throws Exception{
if (age<0) {
throw new Exception("๋์ด๋ 0์ด์๋ง ๊ฐ๋ฅ");
}
this.age = age;
}
}
package ch09.unit01;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Ex17 {
public static void main(String[] args) {
Test17 t = new Test17();
Scanner sc = new Scanner(System.in);
try {
System.out.print("์ด๋ฆ?");
t.setName(sc.next());
System.out.print("๋์ด?");
t.setAge(sc.nextInt());
System.out.println(t.getName()+","+t.getAge());
} catch (InputMismatchException e) {
System.out.println("๋์ด๋ ์ซ์๋ง...");
} catch (Exception e) {
System.out.println("์์ธ ๋ฐ์...");
} finally {
sc.close();
}
}
}
class Test17 {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) throws Exception{
if (name == null || name.length()<2) {
throw new Exception("์ด๋ฆ์ ๋์ ์ด์...");
}
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) throws Exception{
if (age<0) {
throw new Exception("๋์ด๋ 0์ด์...");
}
this.age = age;
}
}
์ฌ์ฉ์ ์ ์ ์์ธ
- ์ฌ์ฉ์์ ์ํด ๋ง๋ค์ด์ง๋ ์์ธ
- ์ฌ์ฉ์ ์ ์ checked ์์ธ ํด๋์ค ์์ฑ. Exception ํด๋์ค๋ฅผ ์์ ๋ฐ์ ์์ฑํ๋ค.
- ์ฌ์ฉ์ ์ ์ unchecked ์์ธ ํด๋์ค ์์ฑ RuntimeException ํด๋์ค๋ฅผ ์์ ๋ฐ์ ์์ฑํ๋ค.
package ch09.unit01;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Ex18 {
public static void main(String[] args) {
Test18 t = new Test18();
Scanner sc = new Scanner(System.in);
try {
System.out.println("์ด๋ฆ?");
t.setName(sc.next());
System.out.println("๋์ด?");
t.setAge(sc.nextInt());
System.out.println(t.getName() + "," + t.getAge());
} catch (InputMismatchException e) {
System.out.println("๋์ด๋ ์ซ์๋ง..");
} catch (NameValidException e) {
System.out.println("์ด๋ฆ์ ์ ํํ..");
} catch (AgeValidException e) {
System.out.println("๋์ด๋ฅผ ์ ํํ..");
} catch (Exception e) {
System.out.println("์์ธ ๋ฐ์..");
} finally {
sc.close();
}
System.out.println("end...");
}
}
// ์ฌ์ฉ์ ์ ์ ์์ธ ํด๋์ค (checked exception)
class NameValidException extends Exception {
private static final long serialVersionUID = 1L;
public NameValidException(String message) {
super(message);
}
}
class AgeValidException extends Exception {
private static final long serialVersionUID = 1L;
public AgeValidException(String message) {
super(message);
}
}
class Test18 {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) throws NameValidException {
if (name == null || name.length() < 0) {
throw new NameValidException("์ด๋ฆ์ ๋์ ์ด์...");
}
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) throws AgeValidException {
if (age < 0) {
throw new AgeValidException("๋์ด๋ 0์ด์");
}
this.age = age;
}
}
package ch09.unit01;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Quiz {
public static void main(String[] args) {
Calculate obj = new Calculate();
obj.calc();
}
}
class OperatorException extends Exception {
private static final long serialVersionUID = 1L;
public OperatorException(String message) {
super(message);
}
}
class Calculate {
private BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
public void calc() {
int a, b;
String op, s;
try {
System.out.print("์ฒซ๋ฒ์งธ ์ ? ");
a = Integer.parseInt(br.readLine());
System.out.print("๋๋ฒ์งธ ์ ? ");
b = Integer.parseInt(br.readLine());
System.out.print("์ฐ์ฐ์ ? ");
op = inputOper();
s = result(a, b, op);
System.out.println(s);
} catch (NumberFormatException e) {
System.out.println("์ซ์๋ง ์
๋ ฅ ๊ฐ๋ฅํฉ๋๋ค.");
} catch (OperatorException e) {
System.out.println("์ฐ์ฐ์ ์
๋ ฅ ์ค๋ฅ ์
๋๋ค.");
} catch (ArithmeticException e) {
System.out.println("0์ผ๋ก ๋๋ ์ ์์ต๋๋ค.");
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
protected String inputOper() throws OperatorException, IOException {
String op = null;
op = br.readLine();
if(! op.matches("\\+|\\-|\\*|\\/")) {
throw new OperatorException("์ฐ์ฐ์ ์
๋ ฅ ์ค๋ฅ");
}
return op;
}
protected String result(int a, int b, String op) {
String s = null;
try {
switch(op) {
case "+" : s = String.format("%d + %d = %d", a, b, a+b); break;
case "-" : s = String.format("%d - %d = %d", a, b, a-b); break;
case "*" : s = String.format("%d * %d = %d", a, b, a*b); break;
case "/" : s = String.format("%d / %d = %d", a, b, a/b); break;
}
} catch (Exception e) {
throw e;
}
return s;
}
}
'๐ป > JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] 16์ผ์ฐจ : ์ปฌ๋ ์ (Collection) (0) | 2024.03.11 |
---|---|
[Java] 15์ผ์ฐจ : ์ ๋๋ฆญ(Generics) (0) | 2024.03.06 |
[Java] 14์ผ์ฐจ : ์์ธ(Exception)์ฒ๋ฆฌ (3) | 2024.03.05 |
[Java] 14์ผ์ฐจ : ์ค์ฒฉํด๋์ค, ์ด๊ฑฐํ, ์ด๋ ธํ ์ด์ , ๋ ์ฝ๋ (0) | 2024.03.05 |
[Java] 14์ผ์ฐจ : ์ธํฐํ์ด์ค(interface) (0) | 2024.03.05 |