Exceptions are errors in the program, but not all errors are exceptions, and errors can sometimes be avoided. For example, your code is missing a semicolon, then running out of the result is the hint is error java.lang.Error; If you use System.out.println (11/0), then you are because you use 0 to do the divisor, Throws a Java.lang.ArithmeticException exception.
Exceptions occur for a number of reasons, usually including the following categories:
- The user entered illegal data.
- The file you want to open does not exist.
- The connection is interrupted during network communication, or the JVM memory overflows.
Some of these exceptions are caused by user errors, due to program errors, and others due to physical errors. To understand how Java exception handling works, you need to master the following three types of exceptions:
- Check Exception: the most representative of the inspection exception is the user error or the exception caused by the problem, 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.
- Run-time Exceptions: runtime Exceptions are exceptions that can be avoided by programmers. Contrary to the inspection exception, run-time exceptions can be ignored at compile time.
- 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.
Hierarchy of the Exception class
- All exception classes are subclasses that inherit from the Java.lang.Exception class .
- The exception class is a subclass of the Throwable class. In addition to the exception class, Throwable also has a subclass error.
Java programs typically do not catch errors. Errors generally occur in the event of a serious failure, which is outside the scope of the Java program processing.
The error is used to indicate that a run-time environment has occurred.
For example, JVM memory overflows. In general, the program does not recover from the error.
The exception class has two primary subclasses: the IOException class and the RuntimeException class.
Java--java exception