Spring Boot Tutorial | How To Handle Exceptions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let's go ahead and then how we can have custom exceptions with spring book so far in our application if I promise just inspect this and then go to console so if i refresh or restart so you can see that right here we have this JSON payload and this is what spring book gives us by default right so you can see that we have an error this is internal server error the message so this message comes from this oops cannot get all students and then we have the path status and also times them but let's say that we want to have custom exceptions and then have a completely different JSON structure so let's go ahead and learn how to do that so open up IntelliJ and let's go ahead and create a new package so we're gonna go ahead and create new package and let's go ahead and call this package as exception now inside of the exception class or actually package go ahead and create a new class and let's go ahead and simply say API and then request and then exception so this will be our custom exception that we can throw throughout our application and now what we need to do is simply extend and then let's go ahead and simply ask extend runtime exception now let's go ahead and create constructor so I'm gonna press ctrl and then enter constructor now go ahead and pick the one that has the actual message so the second one and let's go ahead and also have the constructor that has the actual throwable so we can pass the actual message as well as each trouble and this is pretty much what we need to do for our custom exception now the way that we handle exceptions or actually throw custom exceptions we'd spring both if I simply have an exception handler so let's go ahead and simply create the class and let's go ahead and simply call this as API and then exception and their handler now the purpose of this class is pretty much just to handle custom exceptions or even handle existing exceptions but then we can customize the way that we throw the actual error to the client so if I open up Chrome right here you can see that this is what spring book gives us by default so let's go ahead and change this so you see how it's done so let's go ahead and pretty much have a method and right here simply go ahead and say public and then right here simply say response entity and this will be a response entity of object so right here let's go ahead and simply say a handle and then API request and then exception so basically we're going to handle our custom exception that we've just created so inside what we're going to receive is the actual exception that we've just created so API and then request exception and lesson we call it E and in fact if I put this fullscreen so you see what we are doing now what we need to do is two things so one we need to create payload that we will send inside of the response entity so payload containing exception and then details and then second we need to pretty much return the actual response entity so second return response entity so let's go ahead and pretty much just create a class that will represent the actual exception so let's go ahead and go into your class inside of the exception package and let's go ahead and simply named this as API and then exception just like that go ahead and simply say okay and then inside what we're gonna do is actually a few things so let's go ahead and a few fields so private final and then string and then message let's also go ahead and simply say private final and then we will include the actual throwable so the client can have full visibility of what happened but usually you wouldn't you wouldn't pretty much to just throw this to the client right but let's go ahead and simply throw this so you see how the client can view all the details of the actual exception so next let's go ahead and simply include the actual HTTP status so private final and then HTTP status and then simply name this as HTTP status and finally let's go ahead and seem to say private and then final and then zone daytime and then timestamp right let's also include the actual timestamp so now let's go ahead and simply add these two constructor and if I put this on a new line so you can see exactly what we're doing now let's go ahead and generate some Gators so control enter get her everything and there we go so now we can actually use this API exception so go back to API exception Handler and right here so if we are catching this exception so API request exception let's go ahead and create Neil and then API exception and then inside let's go ahead and pass the actual details of our payload so for the actual message simply go ahead and say e dot and then get a message and then let's go ahead and pass the actual trouble so in our case that will be e so the actual exception and then the HTTP status let's go ahead and simply press shift ctrl and then space and then we can pick from this so let's go ahead and simply say bad request so it's a 400 and finally we have to include the actual timestamp so simply go ahead and say zoned daytime dot and then now and you can pass the actual zone ID so shift control space and then zone ID off so basically I want to have the actual UTC timestamp and there we go and this should be double quotes I believe just like that so now let me go ahead and simply end out semicolon and I can extract this to a variable so I'm going to press alt and then command V and then this is the actual API exception so this object right here contains the actual payload information so currently this is our payload so you can see right here that this is our payload so if I go back so basically this will replace that payload essentially so now what we need to do is simply return the actual response entity so simply go ahead and simply say return and then Neil response entity and then inside let's actually pass the actual API exception and then we also have to pass the status so simply press shift ctrl + space and then request and in fact let's go ahead and extract this to a variable because we have right here and also right here so I want to press option command and then V and then replace all occurrences and then this will be bad request and there we go so we are pretty much done with our exception handler for this specific API request exception so now we have to tell us praying that this method right here will be responsible of handling this exception so the way we do it is by simply adding an annotation right here and simply say add and then exception and then handler now inside of this exception handler we have to pass a value so we can pass one or more values so if you are catching multiple exceptions you can pass some multiple values but in our case what I'm actually catching is the actual API request exception don't and then class so basically this exception right here will be fed inside of this exception class that we've just created and then we can have access to the details and one more last thing that we have to do is so actually tell spraying that this class right here will be served as a class that pretty much handles multiple exceptions so ie you can have multiple exception handlers and the way we instruct Springvale that this class will be served for exception handling is simply by adding an annotation called act and then controller advice and there we go so let's go ahead and test this so what I want to do first is actually stop my server so my stop the server and then I'm gonna add a breakpoint right here so simply add a breakpoint right here and now open up the project tab go to student controller and then right here instead of throwing illegal stale exception let's go ahead and throw our new exception so simply say throw and then Neil and then API request exception and then pass a message so let's simply go ahead and change this to the same message so it's going to be grab this and then we'd and then custom exception just like that and that would say McCallum he remove this illegal set exception and what I'm gonna do now is simply start this in debug mode so go ahead and press on this icon right here for debug just give it a second and there we go so now this is up and running so what I'm going to do is refresh my web browser and if I restart you can see that that takes me right away to this breakpoint because we are throwing this exception right here so we are throwing this exception and then we have this exception handler so right here you can see that we are building the actual contents so it's a bad request so you can see that it's a 400 right here and then we are building this API exception so let me go ahead and simply step over there we go and if I inspect the contents of this API exception you can see that we have a message so oops cannot get all students with custom exception and we also have the actual throwable so this contains every single information and right here you can see that we have the 400 it was a bad request and then we have the actual timestamp so what I'm gonna do is simply continue and you can see that this payload now is sent to our client and let's go ahead and inspect that so right here you can see that we have a new message and there we go so you can see that oops cannot get all students with custom exception bad requests so right here bad request throwable and then the actual timestamp and this is awesome so you see that we have this throw ball right here which contains like a bunch of stuff that we don't need so I just wanted to show you exactly how you can pass stuff around but let's go ahead and remove this so go back to IntelliJ open up API exception remove this trouble right here so command Y to delete there as well there as well and there as well so if I now let me stop the actual process and I wanna run this so instead of debug run Oh actually I need to also remove the actual throw wall right here and then run just give me a second so now if I go back to Chrome and then refresh there we go so you know you can see that we have only three fields the actual status the message right here and the actual timestamp so there we go you now know how to throw custom exceptions with sprinkle if you have any questions go ahead and drop me a message but in my opinion this is a very important topic that you must be aware of and know how to use because eventually you will need to throw custom exceptions throughout your codebase
Info
Channel: Amigoscode
Views: 55,403
Rating: 4.9297071 out of 5
Keywords: Spring Boot Exception Handling, java, REST, java exceptions, exception handling, throw new exception, exception handling in java, exception handling interview questions, exception handling in java program, Spring Boot 2, pivotal, restful apis, json payload spring boot
Id: PzK4ZXa2Tbc
Channel Id: undefined
Length: 14min 11sec (851 seconds)
Published: Sat Apr 20 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.