Create a TODO app in Java 10 A brief summary of exception handling

Hello.

It is a TODO application made with Java + Spring, but since normal processing has been completed, I will explain exception handling next.

TODO application creation link collection

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)

What is exception handling?

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).

Think about what kind of error is expected with this app

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 title at the time of TODO registration is sent with 0 characters or 30 characters or more

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.

Screen Shot 0002-10-03 at 11.57.19.png

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.

Other things that should be dealt with

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.

-The TODO ID specified in the URL does not exist in the DB
An id is required to move to the edit page, but an error is likely to occur when specifying an id that does not exist
-Access other than the specified HTTP method
The HTTP methods used in this app are GET, POST, PUT, PATCH, but other methods will be used to handle requests
・ All errors other than the above (!?)
As I will explain in the next and subsequent articles, this is to combine all the errors that occur in Spring other than the above into one process (transition to the error display page) . Now it's okay to feel like "Hmm, that's right".

Why exception handling

Now, I would like to explain why exception handling is done.

I think there are others, but I have roughly listed two.

You should do it for security.

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 usually scary for users and it's hard to tell what happened (usability is bad)

It's just a matter of showing the user a nightmare error page, even for programmers.

Screen Shot 0002-10-03 at 11.57.19.png

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.

Summary

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

Create a TODO app in Java 10 A brief summary of exception handling
Create a TODO app in Java 7 Create Header
Let's create a TODO application in Java 1 Brief explanation of MVC
Let's create a TODO application in Java 11 Exception handling when accessing TODO with a non-existent ID
Let's create a TODO application in Java 6 Implementation of search function
Let's create a TODO application in Java 5 Switch the display of TODO
Let's create a TODO application in Java 4 Implementation of posting function
Let's create a TODO application in Java 8 Implementation of editing function
[Java] Practice of exception handling [Exception]
A brief summary of loop processing in C language
A brief summary of Linux
Exception handling techniques in Java
Step-by-step understanding of Java exception handling
A brief summary of Python collections
A brief description of JAVA dependencies
Create a new app in Rails
A brief summary of qubits (beginners)
A brief summary of Graphviz in python (explained only for mac)
Create a simple GUI app in Python
Try making a calculator app in Java
A brief summary of DI and DI containers
Create a Python-GUI app in Docker (PySimpleGUI)
A brief summary of Pinax overview #djangoja
Java exception handling
☾ Java / Exception handling
Java exception handling?
Java exception handling
Create a CSR with extended information in Java
Questions in java exception handling throw and try-catch
Let's create a super-simple web framework in Java
[Java] Handling of JavaBeans in the method chain
Try to create a bulletin board in Java
Until you create a new app in Django
Measure the size of a folder in Java
A quick review of Java learned in class
A brief summary of Linux antivirus software for individuals
A quick review of Java learned in class part4
Create a Todo app with Django REST Framework + Angular
How to create a data URI (base64) in Java
I tried to create a Clova skill in Java
A quick review of Java learned in class part3
Create a docker image that runs a simple Java app
Rock-paper-scissors app in Java
[Java] Create a filter
Create JSON in Java
How to create a Java environment in just 3 seconds
Summary of how to implement default arguments in Java
[Programming complete] §5 Create a review management app in Ruby
A quick review of Java learned in class part2
Create a Todo app with Django ⑤ Create a task editing function
The story of embedding Dialogflow in a Java application
Create a Todo app with Django ③ Create a task list page
A brief summary of Rails association options (foreign_key, primary_key)
Create a Todo app with the Django REST framework
Summary of Java support 2018
A brief explanation of a maze game made in Java for a cousin of an elementary school student
Activate Excel file A1 cell of each sheet in Java
A quick explanation of the five types of static in Java
How to develop and register a Sota app in Java
I tried to make a client of RESAS-API in Java
Create a Todo app with Django ① Build an environment with Docker