CodeGym /Java Blog /๋ฌด์ž‘์œ„์˜ /Java๋Š” ์—ฌ๋Ÿฌ ์˜ˆ์™ธ๋ฅผ ํฌ์ฐฉํ•ฉ๋‹ˆ๋‹ค.
John Squirrels
๋ ˆ๋ฒจ 41
San Francisco

Java๋Š” ์—ฌ๋Ÿฌ ์˜ˆ์™ธ๋ฅผ ํฌ์ฐฉํ•ฉ๋‹ˆ๋‹ค.

๋ฌด์ž‘์œ„์˜ ๊ทธ๋ฃน์— ๊ฒŒ์‹œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค
์—ฌ๋Ÿฌ ์˜ˆ์™ธ๋ฅผ ํฌ์ฐฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์šฐ๊ธฐ ์ „์— Java์˜ ๊ธฐ๋ณธ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ์— ๋Œ€ํ•ด ์ž˜ ์•Œ๊ณ  ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค . ์•ž์œผ๋กœ๋Š” Java์˜ try ๋ฐ catch ๋ธ”๋ก์— ์ต์ˆ™ํ•˜๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค.

Java์—์„œ ์—ฌ๋Ÿฌ catch ๋ธ”๋ก์ด ํ•„์š”ํ•œ ์ด์œ ๋Š” ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?

Java์˜ ์—ฌ๋Ÿฌ catch ๋ธ”๋ก์€ ๋‹ค์–‘ํ•œ ์œ ํ˜•์˜ ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. Java 7์ด ์ถœ์‹œ๋˜๊ธฐ ์ „์— ํŠน์ • ์˜ˆ์™ธ๋ฅผ ํฌ์ฐฉํ•˜๋ ค๋ฉด ํŠน์ • catch ๋ธ”๋ก์ด ํ•„์š”ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋กœ ์ธํ•ด ์ค‘๋ณต ์ฝ”๋“œ ๋ธ”๋ก์ด ์ƒ์„ฑ๋˜์–ด ๋น„ํšจ์œจ์ ์ธ ์ ‘๊ทผ ๋ฐฉ์‹์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์žกํžŒ ์˜ˆ์™ธ๋ฅผ ํ™•์ธํ•˜๋ ค๋ฉด ๋‹ค์Œ ์˜ˆ๋ฅผ ์‚ดํŽด๋ณด์‹ญ์‹œ์˜ค. ๋‹ค๋ฅธ ์ข…๋ฅ˜์˜ ์˜ˆ์™ธ์— ๋Œ€ํ•ด ๋ณ„๋„์˜ catch ๋ธ”๋ก์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

๋ณ„๋„์˜ Catch ๋ธ”๋ก์„ ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ


import java.util.Arrays;

public class ExceptionHandler {

	public static void main(String[] args) {

		Integer[] colorsOfASpectrum = { 7, 6, 5, 4, 3, 2, 1, 0 };

		try {

			System.out.println("Total number of options on a dice are: " + Arrays.toString(colorsOfASpectrum));
		
	// 		un-comment the following line to see "Index Out of Bounds Exception"		
	//		colorsOfASpectrum[10] = 7; // Index Out of Bounds Exception
			
			System.out.println(colorsOfASpectrum[0] / 0);	// Arithmetic Exception

		} catch (ArrayIndexOutOfBoundsException e) {
			// This catch block executes in case of "Index Out of Bounds Exception"
			System.out.println("Array Index Out Of Bounds Exception " + e);
		
		} catch (ArithmeticException e) {
			// This catch block executes in case of "Arithmetic Exception"
			System.out.println("Arithmetic Exception " + e);
		}
		
		System.out.println("\n----Rest of the code executes here----");
	}
}

์‚ฐ์ถœ

์ŠคํŽ™ํŠธ๋Ÿผ์˜ ์ด ์ƒ‰์ƒ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. [7, 6, 5, 4, 3, 2, 1] ์˜ˆ์™ธ ๋ฐœ์ƒ java.lang.ArithmeticException: / by zero ----๋‚˜๋จธ์ง€ ์ฝ”๋“œ๋Š” ์—ฌ๊ธฐ์—์„œ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค----
๋ณด์‹œ๋‹ค์‹œํ”ผ ์œ„์˜ ์˜ˆ์—์„œ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ๋‹ค๋ฅธ ๋ธ”๋ก์ด ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์œ ํ˜•์˜ ์˜ˆ์™ธ๋ฅผ ํฌ์ฐฉํ•˜๊ธฐ ์œ„ํ•ด ๋™์ผํ•œ ์ฝ”๋“œ ๋ธ”๋ก์„ ์‚ฌ์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ ์˜ˆ์™ธ๋ฅผ ํฌ์ฐฉํ•˜๋Š” ๋ณด๋‹ค ํšจ์œจ์ ์ธ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ์˜ˆ๋ฅผ ์‚ดํŽด๋ณด์‹ญ์‹œ์˜ค.

Java์—์„œ ๋‹ค์ค‘ Catch ๋ธ”๋ก์„ ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ


import java.util.Arrays;

public class MultiExceptionHandler {

	public static void main(String[] args) {

		Integer[] colorsOfASpectrum = { 7, 6, 5, 4, 3, 2, 1 };

		try {

			System.out.println("Total colors on a spectrum are: " + Arrays.toString(colorsOfASpectrum));

	//		colorsOfASpectrum[10] = 7; // Index Out of Bounds Exception
			System.out.println(colorsOfASpectrum[0] / 0); // Arithmetic Exception

		} catch (ArrayIndexOutOfBoundsException | ArithmeticException e) {
			// We don't need two different catch blocks for different kinds of exceptions
			// Both exceptions will be handled using this multiple catch block

			System.out.println("Exception Encountered " + e);
		}
		System.out.println("\n----Rest of the code executes here----");
	}
}

์‚ฐ์ถœ

๋‹ค๋ฅธ ์ข…๋ฅ˜์˜ ์˜ˆ์™ธ๋ฅผ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด 13ํ–‰์˜ ์ฃผ์„์„ ํ•ด์ œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์ŠคํŽ™ํŠธ๋Ÿผ์˜ ์ด ์ƒ‰์ƒ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. [7, 6, 5, 4, 3, 2, 1] ์˜ˆ์™ธ ๋ฐœ์ƒ java.lang.ArithmeticException: / by zero ----๋‚˜๋จธ์ง€ ์ฝ”๋“œ๋Š” ์—ฌ๊ธฐ์—์„œ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค----

๊ฒฐ๋ก 

์ด๊ฒƒ์€ java catch multiple exceptions ๋ธ”๋ก ์‚ฌ์šฉ์— ๋Œ€ํ•œ ๋น ๋ฅธ ๊ฐœ์š”์˜€์Šต๋‹ˆ๋‹ค. ๋ฌธ์ œ๋กœ ๋ถ€๋ชจ์™€ ์ž์‹ ์˜ˆ์™ธ๋ฅผ ๊ฐ™์€ ๋ธ”๋ก์— ์ธ์‡„ํ•ด ๋ณด์‹ญ์‹œ์˜ค. ์„ฑ์žฅ์„ ์œ„ํ•ด ๋ฐฐ์šฐ๊ณ  ์—ฐ์Šตํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ๊ฑด๋ฐฐ์™€ ํ–‰๋ณตํ•œ ํ•™์Šต!
์ฝ”๋ฉ˜ํŠธ
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION