This is the 24th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

abnormal

Java exception Architecture diagram:

  • The RunTimeException and its subclasses below Error and Exception are exceptions that do not need to be caught by force.
  • Other exceptions and their subclasses are required to force exceptions.

  • Abnormal bodies can pass down.

  • But this is not usually used.

Some suggestions for using exceptions:

  • Use JDK defined exceptions whenever possible!
  • Custom exceptions should be derived from RuntimeException.
  • Define BaseException first, and then subclass it.
  • Custom exceptions should provide multiple constructors.

assertions

When we develop code that looks at certain frameworks, we see something like Assert XXX, which is Java’s assertion mechanism.

The JVM turns off assertion directives by default:

  • Passing the -EA parameter to the Java virtual machine enables assertions
  • Assertions can only be enabled during development and testing

The log

Logging is intended to replace System.out.println(). It can be formatted, redirected to files, and so on. Logs can be archived for easy tracing of problems.

The log of the JDK

Built-in LOGGING (7 logging levels, default: infoSEVERE WARNING INFO (default) CONFIG FINE FINER FINEST)

Set level in the code, you can specify the output of the fixed content.

Disadvantages:

  • The CONFIGURATION file is read and initialized when the JVM starts
  • Configurations cannot be modified after the JVM is started
  • Need to pass parameters in the JVM startup – Djava. Util. Logging. Config. The file = config – file – name

Commons Log

Apache log module is a log module created by Apache.

The one we use most in development is the following Log4J.

Log4J

The most popular way to log

Componentized log system

  • Appenders are used to export logs to different destinations
  • Filter Is used to filter logs
  • Layout Format output format
  • Console and so on

We don’t need to worry about this, just configure it through the configuration file

Log4j can also be implemented through Commons Logging

Using Log4j:

Logging via Commons Logging allows you to use Log4j,

To use Log4j, simply put log4j2.xml and the associated JAR into your classpath,

To replace Log4j, simply remove log4j2.xml and the associated JAR and reference Log4j’s interface only if you extend Log4j

Reference: logging.apache.org/log4j/

Using configuration XML