Exception is an unexpected and unwanted situation occurring in the application.
-> When exception occured our program will terminate abnormally.
-> To achieve graceful termination of the program we need to handle the exception.
-> In Java we have below keywords to handle the exceptions
1) try : It is used to keep risky code
2) catch : Catch block is used to handle the exception
3) throw : It is used to re-throw the exception
4) throws : It is used to ignore the exception
5) finally : It is used to execute clean up logic (closing files, closing connection, release resources....)
Note: When we get exception in REST API we should convey that exception information to client / client application in json format.
Ex:
{
msg : "Exception Reason"
code : "SBI0004"
}
Note: In project, for every exception we will use one CODE i.e exception code.
-> In Spring web mvc we can handle exceptions in 2 ways.
1) Controller Based Exception Handling
- Exception Handlers applicable for only particular controller
2) Global Exception Handling
- Exception Handlers applicable for all the classes in the project
@Data
public class ExceptionInfo {
private String msg;
private String code;
}
------------------------------------------
@RestController
public class DemoRestController {
private Logger logger = LoggerFactory.getLogger(DemoRestController.class);
@GetMapping("/")
public String doAction() {
String msg = "Action in progress";
try {
int i = 10 / 0;
} catch (Exception e) {
logger.error("Exception Occured ::" + e, e);
throw new ArithmeticException(e.getMessage());
}
return msg;
}
@ExceptionHandler(value=ArithmeticException.class)
public ResponseEntity<ExceptionInfo> handleAE(ArithmeticException ae) {
ExceptionInfo exception = new ExceptionInfo();
exception.setMsg(ae.getMessage());
exception.setCode("AIT0004");
return new ResponseEntity<>(exception, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
Comments
Post a Comment