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

๋นˆ ๊ตฌ๋ฉ ์ฑ„์šฐ๊ธฐ

[Java] ์ž๋ฐ” ์˜ˆ์™ธ ์ „๋žต

์ถœ์ฒ˜

https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/Java-Exception-handling-best-practices

 

Java Exception handling best practices

Handling Java exceptions can be tricky. Which Java exceptions should you catch, and which exceptions should you rethrow? And which exceptions can you ignore completely? This is a difficult skill to develop. These Java Exception handling best practices prov

www.theserverside.com

 

https://stackify.com/best-practices-exceptions-java/

 

9 Best Practices to Handle Java Exceptions - Stackify

Handling Java exceptions isn't easy, especially for beginners. Read this post to understand exceptions and best practices for using them.

stackify.com

 

 

์ด๋Ÿฐ ๊ฒƒ๋“ค์„ ์ฃผ์˜ํ•ด์•ผ ํ•˜๋Š”๊ตฌ๋‚˜.. ํ•˜๋Š” ๊ฒƒ๋“ค์„ ์ถœ์ฒ˜๋“ค์–ด์„œ ์ •๋ฆฌํ•จ.


 

1. Be careful what you log. ๋กœ๊ทธํ•˜๋Š” ๊ธฐ๋ก๋ฌผ์— ์ฃผ์˜ํ•˜๋ผ.

๋ณด์•ˆ์„ ์–ด๊ธฐ์ง€ ์•Š๋Š” ๋ฒ”์œ„์—์„œ ๋กœ๊ทธ๋ฅผ ๊ธฐ๋กํ•ด๋ผ. ์•ˆ๊ทธ๋Ÿผ ํ•ด๊ณ ๋œ๋‹ค.

 

2. Don’t bury thrown exceptions. ๋ฐœ์ƒํ•œ ์˜ˆ์™ธ๋ฅผ ๋ฌป์–ด๋‘์ง€ ๋ง์•„๋ผ / Don’t Ignore Exceptions ์˜ˆ์™ธ๋ฅผ ๋ฌด์‹œํ•˜์ง€ ๋งˆ๋ผ

์˜ˆ์™ธ ๋ฐœ์ƒ ์‹œ ์•„๋ฌด ๊ฒƒ๋„ ์•ˆํ•˜๋Š” ๊ฒƒ์„ burying an exception(์˜ˆ์™ธ ๋ฌป๊ธฐ)๋ผ๊ณ  ํ•œ๋‹ค. ์˜ˆ์™ธ๋ฅผ ๋ฌป์œผ๋ฉด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ฌธ์ œ ํ•ด๊ฒฐ์ด ์–ด๋ ค์›Œ์ง„๋‹ค. 

catch ๋ธ”๋ก์—์„œ ์—๋Ÿฌ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ฑฐ๋‚˜ ๋กœ๊ทธ๋ฅผ ๋‚จ๊ธฐ์ง€ ์•Š๋Š” ๊ฒƒ์€ ์˜ˆ์™ธ๋ฅผ ๋ฌด์‹œํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด๊ฑด ์˜ˆ์™ธ๊ฐ€ ๋ฐœํ•˜์ง€ ์•Š์„ ๊ฒƒ์ด๋ž€ ์ƒ๊ฐ์œผ๋กœ ๊ฐœ๋ฐœ์ž๊ฐ€ ์ž‘์„ฑํ–ˆ์„ ๊ฐ€๋Šฅ์„ฑ์ด ํฌ๋‹ค.

 

์ฝ”๋“œ๊ฐ€ ์•ž์œผ๋กœ ์–ด๋–ป๊ฒŒ ๋ฐ”๋€”์ง€ ์•Œ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์—, ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค๋Š” ์‚ฌ์‹ค์„ ์ธ์ง€ํ•˜์ง€ ๋ชปํ•œ์ฑ„๋กœ ๋ˆ„๊ตฐ๊ฐ€๊ฐ€ ์˜ˆ์™ธ ์ด๋ฒคํŠธ๋ฅผ ๋ฐฉ์ง€ํ•˜๋Š” ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋ฅผ ์ œ๊ฑฐํ•  ์ˆ˜๋„ ์žˆ๊ฑฐ๋‚˜, ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๋Š” ์ฝ”๋“œ๊ฐ€ ๋ณ€๊ฒฝ๋˜๋ฉด ๋ชจ๋“  ์˜ˆ์™ธ๋ฅผ catchํ•˜์ง€ ๋ชปํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

์ตœ์†Œํ•œ์œผ๋กœ ์ƒ์ƒํ•  ์ˆ˜ ์—†๋Š” ์ผ์ด ๋ฐœ์ƒํ–ˆ์„ ๋•Œ์— ๋ˆ„๊ตฐ๊ฐ€๊ฐ€ ์ด๋ฅผ ํ™•์ธํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ๋ชจ๋“  ์‚ฌ๋žŒ์—๊ฒŒ ์•Œ๋ฆฌ๋Š” ๋กœ๊ทธ ๋ฉ”์‹œ์ง€๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ํ•˜๋ฉด ๋‹น์‹ ์ด๋‚˜ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์€ ๋กœ๊ทธ๋ฅผ ํ†ตํ•ด ์˜ˆ์™ธ ์ƒํ™ฉ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ๋‹ค.

public void logAnException() {
	try {
		// do something
	} catch (NumberFormatException e) {
		log.error("This should never happen: " + e);
	}
}

 

 

3. Use a global Exception handler. ์ „์—ญ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด๋ผ.

๋Ÿฐํƒ€์ž„ ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ „์—ญ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ(handler)๋ฅผ ์‚ฌ์šฉํ•ด๋ผ. 

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์˜ˆ์™ธ๋ฅผ ๊ธฐ๋กํ•˜๊ณ  ์ž ์žฌ์ ์œผ๋กœ ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๊ณ , ์˜ˆ์™ธ ๋ฐœ์ƒ ์‹œ ์•ฑ์ด ์ค‘๋‹จ๋˜๋Š” ๊ฒƒ์„ ๋ง‰๋Š”๋‹ค.

 

4. Don’t close resources manually. ์ˆ˜๋™์œผ๋กœ ๋ฆฌ์†Œ์Šค๋ฅผ close ํ•˜์ง€ ๋งˆ๋ผ.

try-with-resources, try-catch๋ฅผ ์‚ฌ์šฉํ•˜๋ผ. JVM์ด ์•Œ์•„์„œ ์™„๋ฒฝํ•˜๊ฒŒ ๋ฆฌ์†Œ์Šค๋ฅผ ๋‹ซ๋„๋ก ํ•ด๋ผ.

 

5. Throw early and handle exceptions late. ์˜ˆ์™ธ๋Š” ๋นจ๋ฆฌ throwํ•˜๊ณ  ์ฒ˜๋ฆฌ๋Š” ๋Šฆ๊ฒŒ ํ•˜๋ผ.

์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ์ˆœ๊ฐ„์— ์˜ˆ์™ธ๋ฅผ throw ํ•˜๋ผ.

์˜ˆ์™ธ๋ฅผ catchํ•˜๋Š” ํ•จ์ˆ˜๋Š” ๋ฉ”์†Œ๋“œ์˜ ๋๋ถ€๋ถ„์— ์žˆ์–ด์•ผ ํ•œ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋” ์ ์€ ์ˆ˜์˜ catch ๋ธ”๋ก์„ ์ถ”๊ฐ€ํ•˜๊ณ  ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ ๋ฐ ์œ ์ง€๋ณด์ˆ˜๋ฅผ ์‰ฝ๊ฒŒ ํ•œ๋‹ค. 

 

6. Don’t log and rethrow Java exceptions.

์˜ˆ์™ธ ๋ฐœ์ƒ ์‹œ

1)  ์˜ˆ์™ธ๋ฅผ ๋กœ๊ทธ๋กœ ๋‚จ๊ธฐ๊ณ  ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ณ„์† ์ง„ํ–‰ํ•˜๋˜๊ฐ€

2) ์˜ˆ์™ธ๋ฅผ ๋‹ค์‹œ throwํ•ด์„œ ๋‹ค๋ฅธ ๋ฉ”์†Œ๋“œ๊ฐ€ ์„ธ๋ถ€์‚ฌํ•ญ์„ ๋กœ๊ทธ๋กœ ๋‚จ๊ธฐ๊ฒŒ ๋” ํ•˜๋ผ

๋‘˜ ์ค‘์— ํ•˜๋‚˜๋งŒ ํ•ด์•ผ ํ•œ๋‹ค.

๋‘˜ ๋‹ค ๊ฐ™์ด ํ•ด์„œ๋Š” ์•ˆ๋œ๋‹ค. ๊ทธ๋Ÿผ ์ฝ”๋“œ๊ฐ€ ์ค‘๋ณต๋˜๊ณ  ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ฐพ๊ธฐ ์–ด๋ ต๋‹ค.

 

7. Check for suppressed exceptions. suppressed(์–ต์ œ๋œ) ์˜ˆ์™ธ๋ฅผ ํ™•์ธํ•ด๋ผ.

suppressed(์–ต์ œ๋œ) ์˜ˆ์™ธ๋ฅผ ํ™•์ธํ•ด๋ผ. 

try-with-resource ๊ธฐ๋Šฅ์ด ๋„์ž…๋˜๋ฉด์„œ ๋™์‹œ์— ๋‘ ๊ฐ€์ง€ ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•ด์กŒ๋‹ค. 

 

์˜ˆ์ œ

try ( Door door = new Door() ) {
  door.swing(); /* Throws the SwingException */
}
catch (Exception e) {
  System.out.println("Primary Exception:  " + e.getClass());
  if (e.getSuppressed().length > 0) {
    System.out.print("Suppressed Exception: " + e.getSuppressed()[0]);
  }
}

 

 

8. Explicitly define exception in the throws clause. throws ๊ตฌ๋ฌธ์˜ ์˜ˆ์™ธ๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ์ •์˜ํ•˜๋ผ

throws ๊ตฌ๋ฌธ์—์„œ ๊ตฌ์ฒด์ , ๋ช…์‹œ์ ์œผ๋กœ ์˜ˆ์™ธ๋ฅผ ๊ธฐ์ž…ํ•ด์„œ, ๊ฐœ๋ฐœ์ž๋“ค์ด ํŠน์ • ๋ฉ”์„œ๋“œ๊ฐ€ ์ œ๋Œ€๋กœ ์‹คํ–‰๋˜์ง€ ์•Š์„ ๊ฒฝ์šฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค์–‘ํ•œ ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ ๋ฃจํ‹ด์„ ์•Œ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด๋ผ.

 

9. Catch the most explicit exception first. ๋ช…์‹œ์ ์ธ ์˜ˆ์™ธ๋ฅผ ๋จผ์ € catch ํ•ด๋ผ

๊ตฌ์ฒด์ ์ธ ์˜ˆ์™ธ๋ฅผ ๋จผ์ € catch ํ•˜๊ณ  ๋œ ๊ตฌ์ฒด์ ์ธ ์˜ˆ์™ธ ์ˆœ์œผ๋กœ catch ํ•ด๋ผ. ์ด ๊ทœ์น™์„ ๋”ฐ๋ฅด์ง€ ์•Š์œผ๋ฉด JVM์€ ์ดํ•ดํ•˜๊ธฐ ์–ด๋ ค์›Œํ•œ๋‹ค. 

 

10.Use modern exception handling techniques

Java์—์„œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ๊ด€๋ จํ•œ ๋งŽ์€ ๊ธฐ๋Šฅ๋“ค์ด ์ถ”๊ฐ€๋˜์–ด ์™”๋‹ค. ๋‹จ์ผ catch ๋ธ”๋ก์—์„œ ์—ฌ๋Ÿฌ ์˜ˆ์™ธ๋ฅผ catch ํ•˜๋Š” ๊ธฐ๋Šฅ, try-with-resource ๋ธ”๋ก์œผ๋กœ ๋ฆฌ์†Œ์Šค๋ฅผ ์ž๋™์œผ๋กœ closeํ•˜๋Š” ๊ธฐ๋Šฅ ๋“ฑ์„ ์‚ฌ์šฉํ•ด๋ผ.

 

11.Document the Exceptions You Specify. ์˜ˆ์™ธ๋ฅผ ๋ช…์‹œํ•  ๊ฒฝ์šฐ(throws ๊ตฌ๋ฌธ ์“ธ ๊ฒฝ์šฐ) ๋ฌธ์„œ๋ฅผ ๋‚จ๊ฒจ๋ผ.

 ๋ฉ”์†Œ๋“œ ์‹œ๊ทธ๋‹ˆ์ฒ˜์— ์˜ˆ์™ธ๋ฅผ ๋ช…์‹œํ•  ๊ฒฝ์šฐ(throws ๊ตฌ๋ฌธ ์“ธ ๊ฒฝ์šฐ) Javadoc์„ ์ด์šฉํ•ด ๋ฌธ์„œ๋ฅผ ๋‚จ๊ฒจ๋ผ.

 

์˜ˆ์ œ

/**
* This method does something extremely useful ...
*
* @param input
* @throws MyBusinessException if ... happens
*/
public void doSomething(String input) throws MyBusinessException { ... }

 

12.Throw Exceptions With Descriptive Messages ์„ค๋ช…ํ•˜๋Š” ๋ฉ”์‹œ์ง€์™€ ํ•จ๊ป˜ ์˜ˆ์™ธ๋ฅผ ๋˜์ ธ๋ผ.

์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒ๋œ ๋ฌธ์ œ๋ฅผ ์ดํ•ดํ•˜๋„๋ก ๋•๋Š” ์ •ํ™•ํ•œ ์„ค๋ช…์„ ์ œ๊ณตํ•ด์•ผ ํ•œ๋‹ค. 1-2์ค„๋กœ์งง๊ฒŒ ์“ฐ๋ฉด ๋œ๋‹ค. ์šด์˜ ์‹œ ๋ฌธ์ œ์˜ ์‹ฌ๊ฐ๋„๋ฅผ ์ดํ•ดํ•˜๊ณ  ๋” ์‰ฝ๊ฒŒ ๋ถ„์„ํ•  ์ˆ˜ ์žˆ๋‹ค. 

ํŠน์ • ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ํด๋ž˜์Šค ์ด๋ฆ„์ด ์˜ค๋ฅ˜ ์ข…๋ฅ˜๋ฅผ ์„ค๋ช…ํ•  ๊ฐ€๋Šฅ์„ฑ์ด ๋†’๊ธฐ ๋•Œ๋ฌธ์— ๋งŽ์€ ์ถ”๊ฐ€ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•  ํ•„์š”๋Š” ์—†๋‹ค. 

 

13.Don’t Catch Throwable Throwable์„ catchํ•˜์ง€ ๋งˆ๋ผ.

Throwable์€ ๋ชจ๋“  ์˜ˆ์™ธ์™€ ์—๋Ÿฌ์˜ ์Šˆํผํด๋ž˜์Šค์ด๋‹ค. catch ๊ตฌ๋ฌธ์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ์€ ํ•˜์ง€๋งŒ, ์‚ฌ์šฉํ•˜์ง€ ๋งˆ๋ผ.

catch ์ ˆ์— throwable์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ชจ๋“  ์˜ˆ์™ธ ๋ฟ๋งŒ์ด ์•„๋‹ˆ๋ผ ์—๋Ÿฌ๋„ catchํ•œ๋‹ค. 

์—๋Ÿฌ๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์—†๋Š” ์˜ค๋ฅ˜๋กœ JVM์ด ๋ฐœ์ƒ์‹œํ‚จ๋‹ค. OutOfMemoryError, StackOverflowError๊ฐ€ ์žˆ๋‹ค. ์ด๊ฒƒ๋“ค์€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ œ์–ด ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜์„œ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์—†๋‹ค.

๋”ฐ๋ผ์„œ ์—๋Ÿฌ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฑฐ๋‚˜ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•˜๋Š” ์˜ˆ์™ธ์ ์ธ ์ƒํ™ฉ์— ์ฒ˜ํ•ด ์žˆ๋‹ค๋Š” ํ™•์‹ ์ด ์—†๋‹ค๋ฉด Throwable์„ ์žก์ง€ ์•Š๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.