Location>code7788 >text

The difference between compilation period exception and runtime exception in Java

Popularity:106 ℃/2025-02-22 16:48:26

In Java, exceptions are divided intoRuntime ExceptionandChecked Exception, the core difference between the two isIs the compiler mandatory processing required?. Here is a detailed comparison of them:


1. Definition and classification

category Runtime Exception Checked Exception
Inheritance relationship Inherited fromRuntimeException(As a Unchecked Exception) Inherited directly fromException(But it does not inherit RuntimeException)
Processing requirements No mandatory processing(The compiler does not check) Must be explicitly processed(Catch or throw, otherwise the compilation will be reported)
Design Intent Indicates program logic errors or uncontrollable runtime problems (such as null pointers, arrays that go beyond bounds) Indicates foreseeable exceptions that require active processing by the program (such as file does not exist or network interruption)

2. Common examples

Exception during runtime Compilation period exception
NullPointerException IOException
ArrayIndexOutOfBoundsException SQLException
ClassCastException FileNotFoundException
ArithmeticException(Excerpt from zero error) InterruptedException

3. Comparison of processing methods

(1) Runtime Exception

  • No mandatory processing is required, but it is recommended to actively avoid or capture in the code.
  • If not handled, the exception will propagate upwards, eventually causing the program to crash.
  • Typical scenarios
    // Example: NullPointerException may be thrown
     String str = null;
     (()); // Exception thrown during runtime

(2) Compilation period exception (Checked Exception)

  • Must be explicitly processed(Otherwise, the compilation fails).

  • Two ways of processing

    • Catch exceptionstry-catch
    • Statement thrownthrows
    // Example: IOException must be handled (compilation period check)
     public void readFile() throws IOException { // Method 1: Declare thrown
         FileReader file = new FileReader("");
     }
    
     // or
     public void readFile() {
         try {
             FileReader file = new FileReader("");
         } catch (IOException e) { // Method 2: Capture processing
             ();
         }
     }

4. Summary of core differences

Contrast dimensions Exception during runtime Compilation period exception
Handle coercive force No mandatory processing (the programmer decides whether to handle it at his own discretion) Must handle (or compile failure)
Error Type Program logic errors or uncontrollable runtime problems Foreseeable exceptions caused by external dependencies
Code robustness Rely on programmers to actively prevent (such as empty judgment and verification parameters) Force programmers to deal with potential problems
Typical repair method Fixed through code logic (such as avoiding null pointers and arrays crossing boundaries) Through exception handling or resource management (such as retry, closing the connection)

5. Why is it designed like this?

  • Exception during runtime
    Usually caused by program logic errors (such as null pointer, zero-dividing error), which is a problem that developers should actively avoid. The compiler does not force processing to avoid code redundancy.

  • Compilation period exception
    Indicates possible problems that may occur in the external environment that the program depends on (such as the file does not exist, network interruption). Forced processing is to enable developers to clearly deal with these foreseeable exceptions and improve code reliability.


6. Suggestions in actual development

  1. Exception during runtime

    • Avoid through code logic (such as judging empty and verifying array index).
    • Capture processing at critical locations (such as unified processing at framework level).
  2. Compilation period exception

    • Preference is given to explicit processing (such as retrying file reading).
    • If it cannot be processed, throw up and log the log.
  3. Custom exceptions

    • Business errors are usually defined asRuntimeException(Avoid forced caller processing).
    • The exception that needs to be handled by the caller actively is defined asChecked Exception

A summary of one sentence

  • Exception during runtime: The programmer's fault, the compiler does not force the blame.
  • Compilation period exception:The caller's fault, the compiler's requirements must be blamed! -