简体   繁体   中英

Exception handling | Java Spring boot

I am new to spring. I am currently learning about Exception handling. My Question is, why do you use an Exception for everything (userNotFoundException, nameNotFoundException, nameAlreadyExistsException)? Why not just make a eception for every HttpCode.

I dont Realy see the value of it.

Here userNotFoundException and nameNotFoundException will convey almost almost same message to the end user and usually such scenarios come under 404 (Resource not found). So the end user can either have to create resource (Sign up) or have to try for another resource. If the resource found, end user will get 200 (OK) Success response.

The nameAlreadyExistsException comes when end user tries to create a resource which already exist. Usually It'll come under either 409 (Conflict), 400 (Bad request) or 403 (forbidden - another resource already exists with the same attributes). If the resource is not exist, end user will get 201 (Resource created) Success response.

Imagine as an end user who got 400 for all the above said scenarios end user couldn't understand what went wrong. Did he tried to access a non existing resource or did he tried to create an existing resource.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM