๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป/JAVA

[Java] 15์ผ์ฐจ : ์˜ˆ์™ธ(Exception)์ฒ˜๋ฆฌ 2

by ๋”ฐ๊ถˆ 2024. 3. 6.

 

 

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;
	}
}