10. Spring Exception Handling - II

26 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€65.09
List Price:  €93
You save:  €27.90
£56.02
List Price:  £80.04
You save:  £24.01
CA$96.21
List Price:  CA$137.46
You save:  CA$41.24
A$106.50
List Price:  A$152.16
You save:  A$45.65
S$94.88
List Price:  S$135.55
You save:  S$40.67
HK$547.08
List Price:  HK$781.57
You save:  HK$234.49
CHF 63.59
List Price:  CHF 90.85
You save:  CHF 27.25
NOK kr764.52
List Price:  NOK kr1,092.22
You save:  NOK kr327.70
DKK kr485.54
List Price:  DKK kr693.66
You save:  DKK kr208.12
NZ$116.87
List Price:  NZ$166.97
You save:  NZ$50.09
د.إ257.07
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,696.46
List Price:  ৳10,995.41
You save:  ৳3,298.95
₹5,845.02
List Price:  ₹8,350.38
You save:  ₹2,505.36
RM331.92
List Price:  RM474.20
You save:  RM142.27
₦98,297.45
List Price:  ₦140,430.95
You save:  ₦42,133.50
₨19,467.44
List Price:  ₨27,811.82
You save:  ₨8,344.38
฿2,586.11
List Price:  ฿3,694.61
You save:  ฿1,108.49
₺2,257.70
List Price:  ₺3,225.42
You save:  ₺967.72
B$355.19
List Price:  B$507.43
You save:  B$152.24
R1,300.44
List Price:  R1,857.85
You save:  R557.41
Лв127.53
List Price:  Лв182.19
You save:  Лв54.66
₩95,343.18
List Price:  ₩136,210.38
You save:  ₩40,867.20
₪259.39
List Price:  ₪370.57
You save:  ₪111.18
₱4,014.38
List Price:  ₱5,735.07
You save:  ₱1,720.69
¥10,876.02
List Price:  ¥15,537.84
You save:  ¥4,661.82
MX$1,183.66
List Price:  MX$1,691.01
You save:  MX$507.35
QR254.83
List Price:  QR364.06
You save:  QR109.23
P952.42
List Price:  P1,360.66
You save:  P408.23
KSh9,168.69
List Price:  KSh13,098.69
You save:  KSh3,930
E£3,329.50
List Price:  E£4,756.64
You save:  E£1,427.13
ብር3,992.74
List Price:  ብር5,704.16
You save:  ብር1,711.42
Kz58,583.02
List Price:  Kz83,693.62
You save:  Kz25,110.60
CLP$65,554.03
List Price:  CLP$93,652.63
You save:  CLP$28,098.60
CN¥505.70
List Price:  CN¥722.46
You save:  CN¥216.76
RD$4,071.34
List Price:  RD$5,816.45
You save:  RD$1,745.10
DA9,415.49
List Price:  DA13,451.28
You save:  DA4,035.78
FJ$159.21
List Price:  FJ$227.46
You save:  FJ$68.24
Q544.84
List Price:  Q778.38
You save:  Q233.53
GY$14,671.19
List Price:  GY$20,959.74
You save:  GY$6,288.55
ISK kr9,784.60
List Price:  ISK kr13,978.60
You save:  ISK kr4,194
DH703.12
List Price:  DH1,004.51
You save:  DH301.38
L1,240.96
List Price:  L1,772.88
You save:  L531.91
ден4,012.27
List Price:  ден5,732.06
You save:  ден1,719.79
MOP$564.93
List Price:  MOP$807.08
You save:  MOP$242.14
N$1,294.05
List Price:  N$1,848.73
You save:  N$554.67
C$2,581.34
List Price:  C$3,687.79
You save:  C$1,106.44
रु9,369.68
List Price:  रु13,385.83
You save:  रु4,016.15
S/261.57
List Price:  S/373.68
You save:  S/112.11
K271.48
List Price:  K387.85
You save:  K116.36
SAR262.48
List Price:  SAR374.99
You save:  SAR112.51
ZK1,900.47
List Price:  ZK2,715.07
You save:  ZK814.60
L323.94
List Price:  L462.80
You save:  L138.85
Kč1,630.83
List Price:  Kč2,329.86
You save:  Kč699.02
Ft25,346.53
List Price:  Ft36,210.88
You save:  Ft10,864.35
SEK kr763.26
List Price:  SEK kr1,090.42
You save:  SEK kr327.16
ARS$61,644.55
List Price:  ARS$88,067.42
You save:  ARS$26,422.86
Bs483.68
List Price:  Bs691.01
You save:  Bs207.32
COP$273,058.61
List Price:  COP$390,100.45
You save:  COP$117,041.84
₡35,861.91
List Price:  ₡51,233.50
You save:  ₡15,371.58
L1,732.92
List Price:  L2,475.71
You save:  L742.78
₲523,307.77
List Price:  ₲747,614.58
You save:  ₲224,306.80
$U2,685.15
List Price:  $U3,836.09
You save:  $U1,150.94
zł280.89
List Price:  zł401.29
You save:  zł120.40
Already have an account? Log In

Transcript

This is part two session of handling exception in our spring MVC application. Already we have learned how to handle exception using controller based exception handling and handle exceptions resolver now let us learn how to handle exception using response status and global exception handler. Let me first start with response status. Assuming application you are having multiple controllers. Whenever an exception occurs in the controller, you need to have appropriate exception handlers. We are learned how to create an exception handler.

It is a simple method that is annotated with our correct exception handler with the exception class name. If there are any unhandled exceptions that are not handled by design exception handler in that scenario, the server will throw internal server error that is HTTP 500 response status. If you want to add or give your own response status in that scenario, identify the exception, which is not having a exception handler and annotate the class with a direct response status. Now, when you run your application, if there is an unhandled exception, and if we have added a threat response status, automatically, the response status will be shown in the browser. I have a MVC application in my MVC application in a controller method throws invalid city exception. I have not added a handler to handle this invalid city exception.

So explicitly, I'm adding our permit response status annotation above this class, wherein I am specifying the HTTP status code and the reason for this exception So that's what is given over here coders HTTP status conflict refund invalid city. Now, when you run your application in the browser, you will be getting this error message, what is the response status code at what is the reason for this particular exception. Usually, we will not show these kinds of exceptions to the users, we will be trying to give a proper error error page to the user just like how we are configured using handler exceptions. I will explain this while doing the program. Next global exception handler The name itself specifies This is the handler which is going to handle all the exceptions in a MVC application. So, whenever an exception occurs in any of the controller, automatically it will be handled by the global exception handler.

What is this? It is nothing but a simple class that is annotated with our direct controller advice. In this class, you will be writing many methods that are annotated with accurate exception handlers. You can have specific handlers with a specific exception. That is, for invalid city exception, you can add a handler for SQL exception one handler for ID not for one handler. So for each and every exception, you can have separate handlers.

You can have a common handler for all the exception. Let me show you the example. I have created a class that is annotated with a controller advice. Here I'm adding two methods handle ID exception, and an all exception. Id and the ID exception method can handle only ID not found exception. Handling exception method can handle all the exceptions.

Always remember, when you run an application in which order actually the exceptions will be handled. First it will check whether there is a specific exception handler in the controller. If it is there automatically the exception will be handled by that exception and if it is not available, it is going to search for class that is annotated with at the rate controller advice. And in that class, it will go in search of a method that is annotated with either a exception handler to capture the specific exception. If it not exception is thrown. First we'll check in the controller whether there is any specific exception handler, then it will come to this class because you have annotated with at the rate controller base automatically it'll enter into this method only.

If any other exception like SQL exception is thrown, it will enter into the second method possible checking for a specific exception handlers. The controller if it is not available jumping jumping for global exception handler, if it is not available in the global exception handler, then unless you go in for the response status, you can also have response status of exception handler methods. Let me show you an example. You can see this here I have created one method which is throwing it which is going to handle SQL exception. Together with that I have also added response status up SQL exception is occurring automatically what it is going to do it is just going to take this message and this message will be passed on to the error page. We have discussed till now is given over here in which order the exception handlers will be called, you can just go through this.

Now, let me go back to eclipse we will try how to use response status and global exception handler. I am an excellent I'm going to work with On the same application that we have done in the previous session. Now, I am going to add a few more exceptions that are inbuilt except user defined exceptions. I have created the user defined exception, Id not found exception, invalid city exception, invalid ID exception. Now let me show you the controller, I have modified the code a little bit. In the user controller, we were working with Add User method, which was throwing name not found exception.

Together with that I am trying to throw invalid iD iD not fall if you will, an invalid city exception. Let me show you the logic that I have returned. If the ID is equal to 50, then throw invalid ID exception. If the ID is less than 20. Then throw ID not found exception. If Id is greater than hundred throw SQL exception.

Why when an SQL exception will be thrown, everything related to database only will throw SQL exception, but still I want to show it over here to show the difference between the custom exceptions that we have created and the inbuilt exception that is already available there. SQL exception has an inbuilt exception together with that if the city is not Bangalore throw in marriage city exception. So, this is the logic that I have written over here. Now, what is the exception handler that we are having we are trying to handle only name not found exception for this particular controller that says if name not found exception occurred in this controller, it will be handled by the exception handler. Let me just also add the logger part we are going to use log back So, I will just create the logger. The logger has to be created as a static variable.

So, let me add it next to the class name logger equal to logger factory dot get logger class name, user controller class let me end it with a semicolon get logger is giving me an error. Let me just scroll up and check from where actually this logger factory so important, it is important novel factory from LA for j, I don't want it from lock for j we are working with the SL for j and log back. So, just keep the cursor here and imported from yes and no for j we are using knockbacks framework we are not working with lock for j framework. So select yes or no for j ferrazzi important so under 4g keep the cursor automatically you will be getting the Import Options. Now using this logger Let me try to get the error message name does not exist. dot info user name does not exist.

Now let me save this. Already we have learned how to say this name not found exception for savings. You I want to show this logger message in the console. And then we will move on for the invalid city exception for invalid city exception, I am going to use the response status. For all other exceptions. I'm going to work with global exception handler.

Let me first run this application you can have started this is home dot JSP. Let me click this link, I have got the form this is user form dot JSP. Let me enter an invalid name. Let me click Add User it has to stay back on the same page that is what we have learned in user controller we are trying to redirect it to show form which is nothing but the Red Cross mapping part. Here automatically if you come to the show for a new object is getting created and it is returning user form dot JSP right let me just open the console in the console you will be getting this error message. The last line Can you see user name does not exist.

Right in sub giving info you can even have a test logger dot error automatically to just show you username does not exist. Okay, this is that now let us see for invalid city exception. We don't have any specific exception handler in that controller. But we have created a class that implements what handler exception to server, my exception handler. What it will do it will handle the exception, get the message and call era dot JSP. That's what we have given.

Now let us check that out. I'm going back to the same page may enter the proper name and invalid city facility and simply entering being slow download. Let me click Add User. Automatically it is taking me to Yara dot JSP page because we are having an exception handler that implements handler exception resolver Can you see this only because of this, it is automatically calling this error page. I hope you can understand we have created a handler already. That is why it was captured.

I don't want to have this particular beam. So what I should do, I like to go to the application class. In the application class, I have a method that is annotated with at the rate B, when it was just commented. Now what is the meaning of it? Now this exception handler is a simple Java class. It is not a spring beam, it will not be understood by spring framework.

Now, again, let me run this application I hope you can understand. Even though we didn't create any exception handler in the controller, it was handled automatically because we had a class which implemented handle exceptions with callbacks. So it was taking us to era dot JSP. Now let me rerun this application. I have commented the Beanpot started now again, Let me enter proper values. Raw mean proper name and proper Id just giving it as the click Add User.

Automatically a new window opens. What exactly is the meaning of it? You have created a spring MVC application using spring boots. When you are working with spring boots, if you see in the console, let me just maximize the console. You will be getting all the mapping because done by spring framework, our spring boots, let me just call it the site mapping for show for slash our user. All these are the request mappings that we have added to our methods.

What about the slash here? SlideShare is created by Spring Boot our spring framework to map exceptions. I hope you can understand whenever an exception or error occurs automatically it will go and check whether there is an appropriate JSP page the JSP page should be era dot JSP. If it is that that page will be shown to you, if that page is not there, it will just show you a white label error page or it will show you. What is that? Yeah.

In the browser via HTTP status. Let me just show you that part also. Now I am just minimizing control you awake. Let me click are you sir? This is coming because output is in the form of JSON. And it cannot be shown in Eclipse browser, I have to open the application in the Chrome browser or any other browser.

Let me just copy it and I will open it in the Chrome browser application is open now. The link is there. Let me click the link a water frog and give it a proper name and proper ID for city I'll give I'll just give enter B click Add User. Now you can see what am I getting? I am getting the error page that we have created. Exception page 30 not formatted Yo we can understand actually the Sierra which is which is coming even though we don't have a Candler Cara JC page is getting caught.

It is because Spring Boot has done the mapping for slash error it is just checking in for error dot JSP page. If you are adding era dot JSP page that page will be called automatically and we have got dollar message which is nothing but exception dot get message that is printed over here. Now, what I'm going to do is I'm just going to rename it to EF dot JSP. I don't want the habitat era dot JSP. Let me go back to eclipse. So I'll just close all the other things.

See this is because they are in the form of Jason. So you are to open it only browser other than Eclipse. Now I will just go here I'll rename this era dot JSP to x dot JSP. Okay, Now I will change it. Let me select the application I will read and the application started. I'll go back to the browser and I will open it.

So got the link let me click this link and enter proper name and ID for city a simple B click Add Users. Now can you see this I am getting a white label error page, it is just giving me internal server error. HTTP status is 516 hours parameters. There are methods that we have added within this constructor, new invalid city exception within round brackets whatever we have added is shown over here. We have not added any response status neither we have added any exception handling. So this white label error page is coming.

Now let me show you how to add the response status. Just understand this in the previous case, I had a page a JSP page at Sierra dot JSP. So whenever an exception Even though you don't have exception handler, it is just doing a default error mapping Spring Boot is doing this. That is why you are getting the X. I mean method, the exception page. I didn't call it it was called automatically by Spring Boot, whatever I don't want to call that page I want to give a response status. In that scenario you need to go in for response status.

How I will just show you, I will go back to eclipse now I need to go into invalid city exception about a class I cannot add the rate response status. It has three parameters called for value and research. I'm going to use code and value for record and recent HTTP status dot conflict. These are the response status that are available comma recent results invalid city. Now let me say this, I will read and application In here in Eclipse, I will open it in the browser. Just come let me click the link and add the name and ID for city SB, click Add User.

Now can you see I am getting the HTTP status that the response status understands us. We don't have any page map to slash error. That is why we are getting this white label error page type is conflict status for not make HTTP status code conflict has got the quota for not night. And what is the reason we have given invalid city. If you have got the errata JSP page created automatically that page only will be called and in that page, you will be getting invalid city message. So this is how you use response status.

Now let us learn how to use global exception handler which is much more important. Now I got this message right I'll go to this console and I will also show you did we add any logger for this? No, we have not added any logs. So far we will not get any message. Now let me just create a global exception handler. It is nothing but a simple class, which is going to handle all the exceptions.

Take the class, right click new class global exception handler this class should be annotated with our rate controller advice. Let me add two methods. One method is to handle ID not found exception and another method to handle all other exceptions. Now, let me just go to this user controller I have a method so I will just copy paste it Ctrl C and just come to this global exception handler and let's Ctrl V. again one more control V. I need to add the logs. So lack of time let me just copy paste it in. Ctrl C in global exception handler again, I need to return the first line and what is the class name global exception handler class.

Okay, now there is gone. Okay. What is the exception that I want to handle it not found exception I have got two or two or three different exceptions right. Already we have handled invalid city exception. Now we need to handle it invalid iD iD not found an SQL exception. Now, I will just send it ID not found exception and come to this global exception handler here, I will just pass it and they will give handle ID exception.

And so that info led me to saboteurs logger dot error form. Okay, and, as usual, redirecting it to show form. Next, handle all exception when I want to handle all the other exceptions that SQL exception invalid ID exception everything will be handled over here. So I'll just give a generic exception message exception occurred to change it to yours okay. So, this is a global exception handler, which is going to handle all the exceptions. If it not found exception is occurring automatically it will be taken care by the handler.

If invalid city exception is occurring, the switch will be taken care by handle all exception method you what is that invalid ID exception or SQL exception is happening again it will be taken care by the exception handler. Now, let us try running this application spring MVC exception restart the server patient has started let me click this link and add a proper name and an invalid ID. So if the ID is less than 20 it as to Friday not found exception. And then to this three B C, D data matter we are having multiple if conditions right click Add User automatically the same back in the same page, I will open the console, I have got the error, what is the message you will see global exception handler is being called ID not found. Now, again I will come back here, I have got invalid Id let us use IDs 50 may may have given it as raw ID and just going to give it as 50 City no validation, click Add User, it is just showing the same page and go over here.

Exception occurred. So, if you are having a specific exception handler, it will be handled or that exception will be handled by the handler. If you don't have a specific exception handler, it is going for a generic exception handler. In our case, invalid ID was not having a specific exception handler That is why this method is getting called handle exception method. Can you see that there? It is.

Yes, this one is getting called and it is printing exception occurred and one more than that but let me just I will refresh it now let me add ramen ramen receiving yes in I mean wrong name ID no moderation city no validation let me click Add User it is showing the same page I will go back to this console you can see where which exception handler is getting called the handler that is in user controller that is way and get getting it can you see controller dot user control controller user name does not exist okay. And for city also let us check this out around Cropper id 78. City just be click Add User. Now it is in the same page we have added response status, but still it is not showing the response status it is giving I mean it is coming back to this user form.

However it is possible now You have added a exception handler to handle invalid critiques of chef that is why you are not getting that response status. I will show you in the console. Can you see global exception and that is handling the exception exception occurred okay if you want to get the specific exceptions you can use even called EDA get Givi dot get message where I will just show you I will minimize this console, go to global exception handler here I have got exception occurred right in front of this and pass exception E and now I can pass a dot get message I will save this. Now I will read an application the application has started let me click this link. I will give an invalid city. If we click Add User, now I am in user form the data See the exception has occurred?

Let's just go and check it out. Can you see city not permitted? I ran the application twice that is why I'm getting city not formatted. Similarly for invalid iD iD greater than hundred also we can try raw ID greater than hundred for this. So it is going to throw SQL exception it just does come back to user form dot JSP. Can you see it greater than hundred?

So what actually we have done this we are trying to handle exception in the spring MVC application in different ways. Either you can create handlers for handling custom exceptions in the controller itself. Are we can have a global exception handler. Why do you need a global exception handler if the same exception occurs in different controllers, rather than creating exception handler in all the controllers, you can have it in the generic class that is global exception handlers. You can have any name for that class. But the class should be annotated with at the rate controller at base as many methods that are annotated with accurate exception handlers without for handling appropriate exceptions.

Now, if you want to handle SQL exception or if you want to handle invalid ID exceptions, you create two more methods, so that you will be getting appropriate error messages. If you want you can just direct the user to a error page or you can make the user see the same form again so that he will refill the form again Okay. One more thing like in this scenario, you have to use what let us discuss that which method to use when, for custom exceptions use response status, and method annotated with an exception handler in controllers for inbuilt exception. You have a class that is annotated with at the rate controller advice and add with ADD appropriate exception handlers Are you create a class that employees handler exception resolver unhandled exception. This is all about Exception Handling in spring. Thank you

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.