๐1. try-catch ๋ฌธ๊ณผ throws Exception ์ฌ์ฉ๋ฒ
๊ธฐ๋ณธ์ ์ธ ์์ธ ์ฒ๋ฆฌ๋ ๋ค์ ๋ ๊ฐ์ง ๋ฐฉ๋ฒ์ผ๋ก ๊ตฌํํ ์ ์๋ค.
โ 1-1. try-catch ๋ฌธ
try-catch ๋ฌธ์ ์ฝ๋ ์คํ ์ค ๋ฐ์ํ๋ ์์ธ๋ฅผ ๊ฐ์งํ๊ณ ์ฒ๋ฆฌํ๋ ๊ฐ์ฅ ์ผ๋ฐ์ ์ธ ๋ฐฉ์์ด๋ค.
- ๋ก์ง์ ์ค๋จํ์ง ์๊ณ ์ฆ๊ฐ์ ์ธ ์์ธ ์ฒ๋ฆฌ.
- Spring MVC์์ ์ฃผ๋ก Controller
๐ try-catch ์์
try {
int result = 10 / 0; // ์์ธ ๋ฐ์ (ArithmeticException)
} catch (ArithmeticException e) {
System.out.println("์์ธ ๋ฐ์: " + e.getMessage()); // ์์ธ ๋ฉ์์ง ์ถ๋ ฅ
e.printStackTrace(); // ์คํ ํธ๋ ์ด์ค ์ถ๋ ฅ
} catch (NullPointerException | ArithmeticException e) { // ๋ค์ค catch ๋ธ๋ก
// …
} finally { // finally ๋ธ๋ญ
// ํญ์ ์คํ๋๋ ์ฝ๋ (๋ฆฌ์์ค ๋ฐํ ๋ฑ)
}
โ 1-2. throws Exception
throws ํค์๋๋ ๋ฉ์๋์์ ๋ฐ์ํ ์ ์๋ ์์ธ๋ฅผ ํธ์ถํ ๊ณณ์์ ์ฒ๋ฆฌํ๋๋ก ์์ํ๋ ์ญํ ์ ํ๋ค.
- ์์ธ๋ฅผ ์์ ๋ฉ์๋๋ก ์ ํํ์ฌ ๊นจ๋ํ ์ฝ๋ ๋ฐ ์ ์ฐ์ฑ ๊ฐ์ง(ํธ์ถ ๋ถ ๋ง๋ค ๋ค๋ฅด๊ฒ ์ฒ๋ฆฌ).
- Spring MVC์์ ์ฃผ๋ก Service
๐ throws Exception ์์
public void readFile(String fileName) throws IOException {
FileReader file = new FileReader(fileName); // IOException ๋ฐ์ ๊ฐ๋ฅ
}
๐๋ง์ฝ controller์์๋ throws ํ๋ฉด?
Spring์์ ์ฒ๋ฆฌํ๋ค!! -> ๐3๋ฒ ์ฐธ๊ณ !!
๐ 2. ์์ธ์ ์ข ๋ฅ
์๋ฐ์์ ์์ธ(Exception)๋ ํ๋ก๊ทธ๋จ ์คํ ์ค์ ๋ฐ์ํ ์ ์๋ ์ค๋ฅ ์ํฉ์ ์ฒ๋ฆฌํ๊ธฐ ์ํด ์ฌ์ฉ๋๋ค.
โ 2-1. Exception ๊ณ์ธต - exception์ ์ ๊ฑธ๋ฆฌ๋ ์๋ฌ
์ธ๋ถ api ์๋ฌ ์ค Exception์ ๊ฑธ๋ฆฌ์ง ์๋ ์๋ฌ๊ฐ ์์๋ค!! ์ด๋๋ ์ฒ๋ฆฌ ๋ฐฉ๋ฒ์ ์ฐพ๋ค๊ฐ catch (Throwable t)๋ฅผ ์ฌ์ฉํ๊ฒ ๋์ด์ ๊ณ์ธต์ ๋ํด ์์๋ณด์๋ค.
๐น ์์ธ ๊ณ์ธต ๊ตฌ์กฐ
- ์๋ฐ์ ์์ธ๋ Throwable ๊ณ์ธต ๊ตฌ์กฐ๋ฅผ ๋ฐ๋ฅธ๋ค. Throwable ํด๋์ค๋ ์ต์์ ํด๋์ค๋ก, Error์ Exception์ ํฌํจํ๋ค.
java.lang.Throwable
โโโ Error (์: OutOfMemoryError)
โโโ Exception
โโโ ์ฒดํฌ ์์ธ (Checked Exception): IOException, SQLException ๋ฑ
โโโ ์ธ์ฒดํฌ ์์ธ (Unchecked Exception): RuntimeException, NullPointerException ๋ฑ
๐น Error์ Exception
- Error: ์ฃผ๋ก ์์คํ ์์ค์์ ๋ฐ์ํ๋ ๋ฌธ์ (์: OutOfMemoryError, StackOverflowError ๋ฑ)๋ก, ๋ณต๊ตฌ๊ฐ ๋ถ๊ฐ๋ฅํ ์๋ฌ์ด๋ค.
- Exception: ์ ํ๋ฆฌ์ผ์ด์ ์์ค์์ ๋ฐ์ํ ์ ์๋ ์์ธ์ด๋ค. ์ด ํด๋์ค๋ Throwable์ ์์ํ๋ฉฐ, ์ฒดํฌ ์์ธ(Checked Exception)์ ์ธ์ฒดํฌ ์์ธ(Unchecked Exception)๋ก ๋๋๋ค.
๐น ์์ธ ์ฒ๋ฆฌ
- ์ผ๋ฐ์ ์ผ๋ก catch (Exception e)๋ฅผ ์ฌ์ฉํ์ฌ ์ ํ๋ฆฌ์ผ์ด์ ์์ ๋ฐ์ํ๋ ์์ธ๋ฅผ ์ฒ๋ฆฌํ๋ฉฐ,
- catch (Throwable t)๋ฅผ ์ฌ์ฉํ๋ฉด ์์คํ ์ค๋ฅ๊น์ง ํฌ๊ด์ ์ผ๋ก ์ฒ๋ฆฌํ ์ ์๋ค.
- ํ์ง๋ง Error๋ ๋ณต๊ตฌํ ์ ์๋ ์์คํ ์ค๋ฅ์ด๋ฏ๋ก, ๋๋ถ๋ถ์ ๊ฒฝ์ฐ Exception๋ง ์ฒ๋ฆฌํ๋ ๊ฒ์ด ๋ฐ๋์งํ๋ค.
โ 2-2. ์ฒดํฌ ์์ธ vs ์ธ์ฒดํฌ ์์ธ - ์ ํน์ ์๋ฌ๋ง ์ปดํ์ผ ์ค๋ฅ๊ฐ ๋ ๊น?
์์ธ ์ฒ๋ฆฌ๋ฅผ ํ์ง ์์์ ๋ ์ปดํ์ผ ์ค๋ฅ๊ฐ ๋๋ ๊ฒฝ์ฐ๋ ๋ญ๊น? -> ์ฒดํฌ ์์ธ!!
์๋ฐ์์๋ ์์ธ๋ฅผ ํฌ๊ฒ ์ฒดํฌ ์์ธ(Checked Exception) ์ ์ธ์ฒดํฌ ์์ธ(Unchecked Exception) ๋ก ๊ตฌ๋ถํ๋ค.
๐น ์ฒดํฌ ์์ธ (Checked Exception)
- ์ปดํ์ผ ์์ ์ ์์ธ ์ฒ๋ฆฌ๋ฅผ ๊ฐ์ ํ๋ ์์ธ (์ฒ๋ฆฌ ์ ํ๋ฉด ์ปดํ์ผ ์๋ฌ ๋๋ค!)
- ๋ฐ๋์ try-catch๋ก ์ฒ๋ฆฌํ๊ฑฐ๋ throws๋ก ์์ํด์ผ ํจ
- ๋ํ์ ์ธ ์: IOException, SQLException, FileNotFoundException
- Exception ํด๋์ค ์์
try {
FileReader file = new FileReader("test.txt");
} catch (FileNotFoundException e) {
System.out.println("ํ์ผ์ ์ฐพ์ ์ ์์ต๋๋ค.");
}
๐น ์ธ์ฒดํฌ ์์ธ (Unchecked Exception)
- ์คํ(Runtime) ์ค ๋ฐ์ํ๋ ์์ธ๋ก, ๋ช ์์ ์ธ ์์ธ ์ฒ๋ฆฌ๊ฐ ๊ฐ์ ๋์ง ์์
- ๋ํ์ ์ธ ์: NullPointerException, ArrayIndexOutOfBoundsException, ArithmeticException
- RuntimeException ํด๋์ค ์์
String str = null;
System.out.println(str.length()); // NullPointerException ๋ฐ์
โ 2-3. ์ฃผ์ ์์ธ ์ข ๋ฅ
์์ฃผ ๋ฐ์ํ๋ ์์ธ์ ์ข ๋ฅ์ด๋ค!
์์ธ ํด๋์ค | ์ค๋ช |
IOException | ํ์ผ ์ ์ถ๋ ฅ ๊ณผ์ ์์ ๋ฐ์ํ๋ ์์ธ |
SQLException | ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ์ฐ ์ค ๋ฐ์ํ๋ ์์ธ |
NullPointerException | ๊ฐ์ฒด๊ฐ null์ผ ๋ ๋ฉ์๋๋ฅผ ํธ์ถํ ๊ฒฝ์ฐ ๋ฐ์ |
ArrayIndexOutOfBoundsException | ๋ฐฐ์ด์ ์ ํจ ๋ฒ์๋ฅผ ๋ฒ์ด๋ ๊ฒฝ์ฐ ๋ฐ์ |
ArithmeticException | 0์ผ๋ก ๋๋๋ ์ฐ์ฐ์ ์ํํ ๋ ๋ฐ์ |
๐ 3. ์คํ๋ง์์์ ์์ธ ์ฒ๋ฆฌ ํ๋ฆ - ๋ง์ฝ controller์์๋ throws ํ๋ฉด?
1-3์์์ ์๋ฌธ์ ํ๊ธฐ์ํด ์์๋ณธ ๋ด์ฉ์ด๋ค!!
โ 3-1. ๊ธฐ๋ณธ ์์ธ ์ฒ๋ฆฌ ๋ฐฉ์ (DispatcherServlet์ ์ญํ )
์คํ๋ง์์๋ ๊ธฐ๋ณธ์ ์ผ๋ก DispatcherServlet์ด ์์ธ๋ฅผ ์ฒ๋ฆฌํ๋ค. ์ปจํธ๋กค๋ฌ์์ ์์ธ๊ฐ ๋ฐ์ํ๋ฉด DispatcherServlet์ด ์ด๋ฅผ ๊ฐ์งํ๊ณ ์ ์ ํ HTTP ์๋ต์ ๋ฐํํ๋ค.
- Exception ๋๋ RuntimeException์ด ๋ฐ์ํ๋ฉด 500 Internal Server Error๋ฅผ ๋ฐํ
- ํน์ ์์ธ์ ๋ํ ์ฒ๋ฆฌ๊ฐ ์์ผ๋ฉด ๊ธฐ๋ณธ์ ์ธ ์์ธ ์๋ต์ ํด๋ผ์ด์ธํธ์ ์ ๋ฌ
โ 3-2. @ExceptionHandler์ @ControllerAdvice
์คํ๋ง์์๋ @ExceptionHandler์ @ControllerAdvice๋ฅผ ์ฌ์ฉํด ์ ์ญ ๋๋ ๊ฐ๋ณ ์ปจํธ๋กค๋ฌ ๋จ์์์ ์์ธ๋ฅผ ์ฒ๋ฆฌํ ์ ์๋ค.
๐ @ExceptionHandler (๊ฐ๋ณ ์ปจํธ๋กค๋ฌ์์ ์์ธ ์ฒ๋ฆฌ)
@RestController
public class SampleController {
@GetMapping("/divide")
public int divide(@RequestParam int a, @RequestParam int b) {
return a / b; // b๊ฐ 0์ด๋ฉด ArithmeticException ๋ฐ์
}
@ExceptionHandler(ArithmeticException.class)
public ResponseEntity<String> handleArithmeticException(ArithmeticException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("์๋ชป๋ ๊ณ์ฐ์
๋๋ค: " + e.getMessage());
}
}
๐ @ControllerAdvice (์ ์ญ ์์ธ ์ฒ๋ฆฌ)
@ControllerAdvice๋ฅผ ์ฌ์ฉํ๋ฉด ์ฌ๋ฌ ์ปจํธ๋กค๋ฌ์์ ๋ฐ์ํ๋ ์์ธ๋ฅผ ํ ๊ณณ์์ ์ฒ๋ฆฌํ ์ ์๋ค.
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(NullPointerException.class)
public ResponseEntity<String> handleNullPointerException(NullPointerException e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Null ๊ฐ์ด ๋ฐ์ํ์ต๋๋ค");
}
}
'Backend > JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
static ํค์๋ (0) | 2025.02.26 |
---|---|
JNDI๋? (0) | 2025.02.26 |
[Java] ๋น๋๊ธฐ ์์ ์ฒ๋ฆฌ (ExecutorService VS CompletableFuture) (0) | 2025.02.23 |
HttpURLConnection (0) | 2024.06.07 |
๋ธ๋กํนํ(Blocking Queue) (0) | 2024.06.05 |