Hello.
It is a TODO application made with Java + Spring, but since normal processing has been completed, I will explain exception handling next.
1: [Understanding the super basics] A brief description of MVC 2: [Prepare a template] I want to create a template with Spring Initializr and do Hello world 3: [Connection / Settings / Data display with MySQL] Save temporary data in MySQL-> Get all-> Display on top 4: [POST function] Implementation of posting function 5: [PATCH function] Switch TODO display 6: [Easy to use JpaRepository] Implementation of search function [7: [Common with Thymeleaf template fragment] Create Header] (https://qiita.com/nomad_kartman/items/8c33eca2880c43a06e40) [8: [PUT function] Implementation of editing function] (https://qiita.com/nomad_kartman/items/66578f3f91a422f9207d) [9: [Tweak] Sort TODO display in chronological order + Set due date default to today's date] (https://qiita.com/nomad_kartman/items/5ee2b13a701cf3eaeb15) 10: [Exception handling in spring] A brief summary of exception handling (here and now)
An error is always encountered while programming.
Implement something and start the app ... Error screen.
It's a common story to fix that error and say another error ...
The "exception" in exception handling is the error here.
In other words, what to do when an unexpected event (= exception) occurs is exception handling (manma).
Even if you understand what exception handling is like as a concept, you don't know how to use it!
So, let's first consider the errors that are likely to occur with this TODO app!
The TODO table this time was generated with the following query statement! (See Create a TODO app in Java 3 Save temporary data in MySQL-> Get all-> Display on top)
CREATE TABLE `todo` (
`id` bigint(11) unsigned zerofill NOT NULL AUTO_INCREMENT COMMENT 'ID',
`title` varchar(30) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT 'title',
`deadline` date NOT NULL COMMENT 'Deadline',
`status` tinyint(1) NOT NULL DEFAULT '0' COMMENT 'status',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Creation date and time',
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Update date and time',
PRIMARY KEY (`id`),
UNIQUE KEY `title` (`title`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
What I want to pay attention to here
`title` varchar(30) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT 'title'
This sentence
The title column of TODO is varchar (30)
and is NOT NULL
.
This means that the maximum number of characters is ** 30 characters ** and ** null is not allowed **.
Therefore, even if you request to save to SQL with the title of 30 characters or more or Null, it will fail.
If you try to save TODO with 30 characters or more, you will see this page.
In the 5th line from the top
could not execute statement; SQL...
Is displayed, but it is clearly stated that the SQL could not be executed.
In the second line
This application has no explicit mapping for /error, so you are seeing this as a fallback.
"You haven't set (mapped) the error page to be displayed by yourself, so let's display this page!"
Is saying.
Apparently, it seems necessary to take measures to prevent this error on the 5th line from occurring and to display the error page that you set yourself.
The explanation above is, of course, just one of the huge number of possible errors! So I tried to summarize the corresponding parts in this exception handling.
Now, I would like to explain why exception handling is done.
I think there are others, but I have roughly listed two.
If you do not handle the exception as it is, the White label Error Page will be displayed every time something goes wrong with the application.
The above image shows only a part, but in reality, the mysterious English text is displayed in a row.
This shows the cause of the error and the location where the error occurred, but by reading these, you can see how the app is structured.
That could give a malicious person room to attack!
It is good not to display these extra sentences to improve the safety of the application.
It's just a matter of showing the user a nightmare error page, even for programmers.
For example, the error page I mentioned earlier
If you're a programmer, this page may give you an idea of why the error happened.
General users will not be able to read that "an error has occurred because you have entered more than 30 characters".
It is much more kind to give a guide like "Please enter TODO within 30 characters" in red letters rather than such a scary page.
I briefly summarized the exception handling, but how was it?
You can make your app safe and convenient by handling exceptions.
You may not get used to it at first, but it is a very important part in application production, so you can take it slowly, so let's study it well!
I will explain how to actually write exception handling in the next and subsequent articles.
Recommended Posts