Abnormal
An exception is a run-time error that occurs when a program begins execution.
1 Check Exception: The most representative of the inspection exception is the user error or the problem caused by the exception, which is not foreseen by the programmer. For example, to open a nonexistent file, an exception occurs, and these exceptions cannot be simply ignored at compile time.
2 Runtime Exceptions: Run-time exceptions are exceptions that can be avoided by programmers. Contrary to the inspection exception, run-time exceptions can be ignored at compile time.
3 Error: The error is not an exception, but a problem that is out of the programmer's control. Errors are usually ignored in the code. For example, when a stack overflows, an error occurs, and they are not checked for compilation.
Null pointer exception NullPointerException
Array subscript out-of-bounds exception arrayindexoutofboundsexecption
Type Conversion Exception classcastexception
Arithmetic Exception ArithmeticException
Exception method
The following list is the primary method of the Throwable class:
Check exception: File exception IOException SQL exception SqlException
Throw exception: Exception event occurred during Java program execution, can generate an exception class, the exception class encapsulates the information of the exception event and will be submitted to the Java Runtime System, the system is printed directly by default.
Catch exception: When the Java Runtime system receives an exception object, it looks for code that can handle the exception and puts the current exception object in processing, a process known as catching an exception.
Try-catch and Try-catch-finally
try{
Some ways to throw an exception
}catch (Exception e) {
The code block that handles the exception is from the subclass to the parent class
}catch (Exception2 e) {
}finally {
The code that will eventually execute
}
The finally keyword is used to create a block of code that executes after a try code block.
The code in the finally code block is always executed, regardless of whether an exception occurs.
Throw throws
If a method does not capture an inspection exception, the method must be declared using the throws keyword. The throws keyword is placed at the tail end of the method signature.
You can also throw an exception with the Throw keyword, whether it's newly instantiated or just captured.
Declare any exceptions that will be thrown (declarations)
public void Method Name (parameter list)
Throws Exception category {
Call the method that throws the exception or:
throw new Execption ();
}
Declaring custom exceptions
In Java, you can customize exceptions. Here are some things to keep in mind when writing your own exception classes.
All exceptions must be subclasses of Throwable.
If you want to write an inspection exception class, you need to inherit the Exception class.
If you want to write a run-time exception class, you need to inherit the RuntimeException class.
Generic exception
Two types of exceptions and errors are defined in Java.
JVM (Java Virtual machine) exception: An exception or an error thrown by the JVM. For example: NullPointerException class, ArrayIndexOutOfBoundsException class, ClassCastException class.
Program-Level Exceptions: Exceptions thrown by programs or API programs. such as the IllegalArgumentException class, the IllegalStateException class.
Summary of exceptions
1 when running the abnormal program, logic is used to avoid the simultaneous auxiliary try-catch processing.
2 after the multiple catch block, you can add a catch (Exception) to handle the exceptions that may be missed
3 for indeterminate code, you can also add Try-catch to handle potential exceptions
4 try to deal with exceptions, do not simply call Printstacktrace () to print output
5 How to deal with exceptions, depending on the business needs and the type of exception to decide
6 try to add a finally statement to free up resources (database)
7 overriding methods need to throw exceptions that are consistent with the type of exception thrown by the original method or do not throw exceptions
Java Basics-Exceptions