0% found this document useful (0 votes)
4 views2 pages

Exception Handling in REST API

The document discusses exception handling in REST APIs, emphasizing the importance of managing unexpected situations to prevent abnormal program termination. It outlines Java keywords for exception handling, such as try, catch, throw, throws, and finally, and explains how to convey exception information to clients in JSON format. Additionally, it describes two methods for handling exceptions in Spring web MVC: Controller Based Exception Handling and Global Exception Handling, along with a code example demonstrating these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Exception Handling in REST API

The document discusses exception handling in REST APIs, emphasizing the importance of managing unexpected situations to prevent abnormal program termination. It outlines Java keywords for exception handling, such as try, catch, throw, throws, and finally, and explains how to convey exception information to clients in JSON format. Additionally, it describes two methods for handling exceptions in Spring web MVC: Controller Based Exception Handling and Global Exception Handling, along with a code example demonstrating these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#############################

Exception Handling In REST API


#############################

-> Exception is an unexpected and unwanted situation occuring 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);
}
}
-----------------------------------------------------------------------------------
-------------------------------

You might also like