Introduction to Exception and the use of transactions

Introduction to Exception and the use of transactions

1, Java exceptions: Error and Exception

1. Overview of exception mechanism

The exception mechanism refers to how the program handles when an error occurs in the program.
Specifically, the exception mechanism provides a safe channel for the program to exit.
When an error occurs, the process of program execution changes, and the control of the program is transferred to the exception handler.

There are three types of program errors:
1. Compilation error
The compilation error is because the program does not follow the syntax rules. The compiler can find and prompt us the cause and location of the error. This is also the most common problem we encounter when we first come into contact with the programming language.
2. Runtime error
The runtime error is because the runtime environment finds an operation that cannot be performed when the program is executing
3. Logic error
Logical error is because the program does not execute in the expected logical order. Exception refers to the errors that occur when the program is running, and exception handling is to handle and control these errors.

2. Abnormal structure

In Java, all exceptions have a common ancestor, Throwable.
Throwable specifies the commonality of any problems that can be transmitted through the Java application through the exception propagation mechanism available in the code.


Is an Exception that the program itself can handle. The Exception class has an important subclass RuntimeException. The RuntimeException class and its subclasses represent errors caused by "JVM common operations".
For example, if you try to use a null object reference, divide by zero, or array out of bounds, run-time exceptions (NullPointerException, arithmetexception) and ArrayIndexOutOfBoundException are thrown, respectively.

Exception s are divided into two categories: runtime exceptions and non runtime exceptions (compilation exceptions)
The program should handle these exceptions as much as possible.
1. Runtime exceptions: all are exceptions of RuntimeException class and its subclasses, such as NullPointerException (null pointer exception), indexoutofboundsexception (subscript out of bounds exception), etc. these exceptions are exceptions that are not checked, and the program can choose to capture and handle them or not. These exceptions are generally caused by program logic errors. The program should avoid such exceptions as much as possible from the perspective of logic. The characteristic of runtime exception is that the Java compiler will not check it, that is, when this kind of exception may appear in the program, it will be compiled even if it is not caught with the try catch statement or thrown with the throw clause declaration.
2. Non runtime Exception (compilation Exception): it is an Exception other than RuntimeException, which belongs to Exception class and its subclasses. From the perspective of program syntax, it is an Exception that must be handled. If it is not handled, the program cannot be compiled. Such as IOException, SQLException, and user-defined Exception exceptions. Generally, exceptions are not checked by users.

2, Custom exception

1. First write a custom exception and inherit RuntimeException. The code is as follows

public class CustomException extends RuntimeException
    private static final long serialVersionUID = 1L;

    private Integer code;

    private String message;

    public CustomException(String message)
        this.message = message;

    public CustomException(String message, Integer code)
        this.message = message;
        this.code = code;

    public CustomException(String message, Throwable e)
        super(message, e);
        this.message = message;

    public String getMessage()
        return message;

    public Integer getCode()
        return code;

2. Transaction function

In the definition of IAuditingService interface:

AjaxResult updateAuditingInfo(AuditingUpdateDto updateDto);

Functions in the implementation class AuditingServiceImpl:

//Functions use transactions
@Transactional(rollbackFor = Exception.class)
public AjaxResult updateAuditingInfo(AuditingUpdateDto updateDto) throws CustomException {
        Auditing auditPojo = auditingMapper.selectAuditingById(updateDto.getAuditingId());

		if (null == auditPojo) {
            throw new CustomException("Object is empty");
        //Modifying data table of auditing
	    int i = auditingMapper.updateHigAuditing(auditPojo);
	    if (0 >= i) {
            throw new CustomException("change auditing Table failed");
		//Update user data
		int rowU = userMapper.updateHigUser(auditPojo);
        if (0 >= rowU) {
            throw new CustomException("change user Table failed");

When the conditions are met, exceptions will run out, the transaction will be rolled back, and the whole function will not be executed to ensure the atomicity of the function.

Java exceptions: Error and Exception
Custom exception build

Keywords: Java Database MySQL

Added by Donny Bahama on Sat, 19 Feb 2022 05:03:39 +0200