๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java

[Java-๊ธฐ์ดˆ] ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ๋ฏธ๋ฃจ๊ธฐ

by ์ฝ”๋”ฉํ•˜๋Š” ๋ถ•์–ด 2021. 2. 22.
๋ฐ˜์‘ํ˜•

โ€‹โ–ถ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ๋ฏธ๋ฃจ๊ธฐ

โ€‹

*์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ๋ฏธ๋ฃจ๋Š” throws ์‚ฌ์šฉํ•˜๊ธฐ

์˜ˆ์™ธ๋ฅผ ํ•ด๋‹น ๋ฉ”์„œ๋“œ์—์„œ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š๊ณ  ๋ฏธ๋ฃฌ ํ›„ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๋ถ€๋ถ„์—์„œ ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•.

-15ํ–‰์— ์˜ค๋ฅ˜๊ฐ€ ๋œฌ๋‹ค

-์ด๊ฑธ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์€

1. ์—ฌ๋Ÿฌ ์˜ˆ์™ธ๋ฅผ ํ•œ๋ฒˆ์— ์ฒ˜๋ฆฌํ•˜๊ธฐ

2. ์˜ˆ์™ธ ์ƒํ™ฉ๋งˆ๋‹ค ์ฒ˜๋ฆฌํ•˜๊ธฐ

3. throws ๋ฉ”์„œ๋“œ ์ถ”๊ฐ€ํ•˜๊ธฐ (์˜ˆ์™ธ ์ฒ˜๋ฆฌ ๋ฏธ๋ฃจ๊ธฐ) ๊ถŒ์žฅ ์‚ฌํ•ญ์€ ์•„๋‹˜. ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋Œ€๋ถ€๋ถ„์˜ ํ”„๋กœ๊ทธ๋žจ์ด ๋น„์ •์ƒ ์ข…๋ฃŒ๋จ.

๊ฐ€ ์žˆ๋‹ค.

โ€‹

์—ฌ๋Ÿฌ ์˜ˆ์™ธ๋ฅผ ํ•œ๋ฒˆ์— ์ฒ˜๋ฆฌํ•จ

 

 

๊ฐ ์˜ˆ์™ธ๋งˆ๋‹ค ๋‹ค๋ฅธ ๋ฐฉ์‹์œผ๋กœ ์ฒ˜๋ฆฌํ•จ

 

โ€‹

โ€‹

*๋‹ค์ค‘ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ

์—ฌ๋Ÿฌ catch๋ฌธ์„ ํ•œ๊บผ๋ฒˆ์— ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ์— ๊ฐ catch ๋ธ”๋ก์€ ๊ฐ๊ฐ์˜ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ๋‹ด๋‹นํ•œ๋‹ค.

์–ด๋–ค ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ• ์ง€ ๋ฏธ๋ฆฌ ์•Œ์ˆ˜ ์—†์ง€๋งŒ ๋ชจ๋“  ์˜ˆ์™ธ ์ƒํ™ฉ์„ ์ฒ˜๋ฆฌํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด ๋งจ ๋งˆ์ง€๋ง‰ ๋ถ€๋ถ„์— Exception ํด๋ž˜์Šค๋ฅผ ํ™œ์šฉํ•˜์—ฌ catch ๋ธ”๋ก์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

package exception;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class ThrowsException {
	public Class loadClass(String fileName, String className) throws FileNotFoundException, ClassNotFoundException {
		FileInputStream fis = new FileInputStream(fileName);
		Class c = Class.forName(className);
		return c;
	}

	public static void main(String[] args) {
		ThrowsException test = new ThrowsException();
		try {
			test.loadClass("a.txt", "java.lang.String");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

-Exception ํด๋ž˜์Šค๋Š” ๋ชจ๋“  ์˜ˆ์™ธ ํด๋ž˜์Šค์˜ ์ตœ์ƒ์œ„ ํด๋ž˜์Šค์ด๋‹ค. ๋”ฐ๋ผ์„œ ๋‹ค๋ฅธ catch ๋ธ”๋ก์— ์„ ์–ธํ•œ ๊ฒƒ ์ด์™ธ์˜ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๋”๋ผ๋„ Exception ํด๋ž˜์Šค๋กœ ์ž๋™ ํ˜• ๋ณ€ํ™˜๋œ๋‹ค.

โ€‹

โ€‹

โ€‹

*๋‹ค์ค‘ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ์—์„œ ์ฃผ์˜ ์‚ฌํ•ญ

์˜ˆ์™ธ๋Š” catch๋ฌธ์„ ์„ ์–ธํ•œ ์ˆœ์„œ๋Œ€๋กœ ๊ฒ€์‚ฌํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ๋งจ ์œ„์— catch(Exception e) ๋ฌธ์žฅ์„ ์“ฐ๋ฉด ๋ฐœ์ƒํ•˜๋Š” ๋ชจ๋“  ์˜ˆ์™ธ ํด๋ž˜์Šค๋Š” Exception ์ƒ์œ„ ํด๋ž˜์Šค๋กœ ์ž๋™ ํ˜• ๋ณ€ํ™˜๋˜์–ด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

๋”ฐ๋ผ์„œ ๊ธฐ๋ณธ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋Š” Exception ํด๋ž˜์Šค ๋ธ”๋ก์€ ์—ฌ๋Ÿฌ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ๋ธ”๋ก์˜ ๊ฐ€์žฅ ์•„๋ž˜์— ๋†“์—ฌ์•ผ ํ•œ๋‹ค.

โ€‹

โ€‹

โ€‹

*์‚ฌ์šฉ์ž ์ •์˜ ์˜ˆ์™ธ

ํ”„๋กœ๊ทธ๋žจ ๊ฐœ๋ฐœ ์ƒํ™ฉ์—์„œ ํ•„์š”์— ๋”ฐ๋ผ ์‚ฌ์šฉ์ž ์ •์˜ ์˜ˆ์™ธ ํด๋ž˜์Šค๋ฅผ ์ง์ ‘ ๋งŒ๋“ค๊ณ  ์ด๋ฅผ ๋ฐœ์ƒ์‹œ์ผœ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ฐ€์žฅ ์ƒ์œ„ ํด๋ž˜์Šค์ธ Exception ํด๋ž˜์Šค์—์„œ ์ƒ์†๋ฐ›๋Š”๊ฒŒ ์ข‹๋‹ค.

์‚ฌ์šฉ์ž ์ •์˜ ์˜ˆ์™ธ ๊ตฌํ˜„ํ•˜๊ธฐ

โ€‹

package exception;

public class IDFormatTest {
	private String userID;

	public String getUserID() {
		return userID;
	}

    // ์•„์ด๋””์— ๋Œ€ํ•œ ์ œ์•ฝ์กฐ๊ฑด ๊ตฌํ˜„
	public void setUserID(String userID) throws IDFormatException { // IDFormatException ์˜ˆ์™ธ๋ฅผ setUserID() ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋ ๋•Œ ์ฒ˜๋ฆฌํ•˜๋„๋ก ๋ฏธ๋ฃธ
		if (userID == null) {
			throw new IDFormatException("์•„์ด๋””๋Š” null์ผ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค"); 
		} else if (userID.length() < 8 || userID.length() > 20) {
			throw new IDFormatException("์•„์ด๋””๋Š” 8์ž ์ด์ƒ 20์ž ์ดํ•˜๋กœ ์“ฐ์„ธ์š”");
		}
		this.userID = userID;
	}

	public static void main(String[] args) {
		IDFormatTest test = new IDFormatTest();

		String userID = null;
		try { // ์•„์ด๋”” ๊ฐ’์ด null์ธ ๊ฒฝ์šฐ
			test.setUserID(userID);
		} catch (IDFormatException e) {
			System.out.println(e.getMessage());
		}

		userID = "1234567";
		try { // ์•„์ด๋”” ๊ฐ’์ด 8์ž ์ดํ•˜์ธ ๊ฒฝ์šฐ
			test.setUserID(userID);
		} catch (IDFormatException e) {
			System.out.println(e.getMessage());
		}
	}
}

โ€‹<์‹คํ–‰ ๊ฒฐ๊ณผ>

โ€‹

โ€‹

โ˜… ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ํ•  ๋•Œ๋Š” ๋กœ๊ทธ๋ฅผ ์ž˜ ๋‚จ๊ธฐ์ž.

์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ๋กœ๊ทธ๋ฅผ ๋ณด๊ณ  ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ๋”ฐ๋ผ๊ฐ€๋ฉฐ ํ™•์ธํ•  ์ˆ˜ ์žˆ๊ณ  ์›์ธ์„ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.

โ€‹

โ€‹

โ€‹

๋‚˜ ํ˜ผ์ž ์ฝ”๋”ฉ) ๋น„๋ฐ€๋ฒˆํ˜ธ ์˜ˆ์™ธ ํด๋ž˜์Šค ๋งŒ๋“ค๊ธฐ

PasswordException์„ ๋งŒ๋“ค์–ด ๋ณด์ž. ์˜ˆ์™ธ ์ƒํ™ฉ์€ ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ null์ธ ๊ฒฝ์šฐ, ๋ฌธ์ž์—ด๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ๊ฒฝ์šฐ, 5์ž ์ดํ•˜์ธ ๊ฒฝ์šฐ์ด๋‹ค.

ํžŒํŠธ) ๋ฌธ์ž์—ด๋กœ๋งŒ ์ด๋ฃจ์–ด์กŒ๋Š”์ง€ ์•Œ์•„๋ณด๋ ค๋ฉด String์˜ matches( ) ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

โ€‹

 

<๋‚ด๊ฐ€ ํ•œ ๊ฒƒ>

โ€‹

PasswordException.java (์‚ฌ์šฉ์ž ์ •์˜ ์˜ˆ์™ธ ๊ตฌํ˜„)

package test;

public class PasswordException extends Exception {
	public PasswordException(String password) {
		super(password);
	}
}

 

 

PasswordTest.java

package test;

public class PasswordTest {
	private String password;

	public String getpassword() {
		return password;
	}

	public void setPassword(String password) throws PasswordException {

		// ์กฐ๊ฑด์‹
		if (password == null) {
			throw new PasswordException("๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” null์ผ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.");
		} else if (password.length() < 6) {
			throw new PasswordException("๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” 6์ž๋ฆฌ ์ด์ƒ์œผ๋กœ ๋งŒ๋“œ์„ธ์š”.");
		} else if (password.matches("[a-zA-Z]+")) { // ์ •๊ทœ์‹
			throw new PasswordException("๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” ์˜๋ฌธ์ž๋‚˜ ์ˆซ์ž ์กฐํ•ฉ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค.");
		}

		this.password = password; // ์œ„ ์กฐ๊ฑด์‹์„ ๋‹ค ๋งŒ์กฑํ•˜๋ฉด password ๊ฐ’ ํ• ๋‹น
	}

	public static void main(String[] args) {
		PasswordTest test = new PasswordTest();

		// ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ null์ผ ๋•Œ
		String password = null;
		try {
			test.setPassword(password);
		} catch (PasswordException e) {
			System.out.println(e.getMessage());
		}
		System.out.println(test.getpassword());

		// ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ 6์ž๋ฆฌ ์ด์ƒ์ด์ง€๋งŒ ๋ฌธ์ž์—ด์ผ ๋•Œ
		password = "abcdefg";
		try {
			test.setPassword(password);
		} catch (PasswordException e) {
			System.out.println(e.getMessage());
		}
		System.out.println(test.getpassword());

		// ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ 6์ž๋ฆฌ ๋ฏธ๋งŒ์ผ ๋•Œ
		password = "abcd";
		try {
			test.setPassword(password);
		} catch (PasswordException e) {
			System.out.println(e.getMessage());
		}

		// ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•  ๋•Œ
		password = "abc123";
		try {
			test.setPassword(password);
		} catch (PasswordException e) {
			System.out.println(e.getMessage());
		}
	}
}

<์‹คํ–‰ ๊ฒฐ๊ณผ>

 

๋”๋ณด๊ธฐ

โ€‹<์„ ์ƒ๋‹˜์ด ํ•œ ๊ฒƒ>

package test;

public class PasswordTest {
	private String password;

	public String getpassword() {
		return password;
	}

	public void setPassword(String password) throws PasswordException {

		// ์กฐ๊ฑด์‹
		if (password == null) {
			throw new PasswordException("๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” null์ผ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.");
		} else if (password.length() < 6) {
			throw new PasswordException("๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” 6์ž๋ฆฌ ์ด์ƒ์œผ๋กœ ๋งŒ๋“œ์„ธ์š”.");
		} else if (password.matches("[a-zA-Z]+")) { // ์ •๊ทœ์‹
			throw new PasswordException("๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” ์˜๋ฌธ์ž๋‚˜ ์ˆซ์ž ์กฐํ•ฉ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค.");
		}

		this.password = password; // ์œ„ ์กฐ๊ฑด์‹์„ ๋‹ค ๋งŒ์กฑํ•˜๋ฉด password ๊ฐ’ ํ• ๋‹น
	}

	public static void main(String[] args) {
		PasswordTest test = new PasswordTest();

		// ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ null์ผ ๋•Œ
		String password = null;
		try {
			test.setPassword(password);
		} catch (PasswordException e) {
			System.out.println(e.getMessage());
		}
		System.out.println(test.getpassword());

		// ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ 6์ž๋ฆฌ ์ด์ƒ์ด์ง€๋งŒ ๋ฌธ์ž์—ด์ผ ๋•Œ
		password = "abcdefg";
		try {
			test.setPassword(password);
		} catch (PasswordException e) {
			System.out.println(e.getMessage());
		}
		System.out.println(test.getpassword());

		// ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ 6์ž๋ฆฌ ๋ฏธ๋งŒ์ผ ๋•Œ
		password = "abcd";
		try {
			test.setPassword(password);
		} catch (PasswordException e) {
			System.out.println(e.getMessage());
		}
		System.out.println(test.getpassword());

		// ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•  ๋•Œ
		password = "abc123";
		try {
			test.setPassword(password);
		} catch (PasswordException e) {
			System.out.println(e.getMessage());
		}
		System.out.println(test.getpassword());
	}
}

<์‹คํ–‰ ๊ฒฐ๊ณผ>

โ€‹

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€