Exception and Error

   One. The difference between Exception and Error
   Both Exception and Error inherit the Throwable class. In Java, only instances of the Throwable type can be thrown (Throw) or catch (catch), it is the basic component type of exception handling mechanism.
  

  Exception is an unexpected situation that can be expected during the normal operation of the program. It may and should be caught and dealt with accordingly.
  

   Error refers to a situation that is unlikely to occur under normal conditions. Most errors will cause the program (such as the JVM itself) to be in abnormal, Unrecoverable state. Since it is an abnormal situation, it is inconvenient and does not need to be captured.
  Exception is divided into checked exceptions and unchecked exceptions (ie compile-time exceptions and runtime exceptions). The checked exceptions must be in the source code Explicit capture processing is part of the compile-time check. Unchecked exceptions, that is, runtime exceptions, are usually logic errors that can be avoided by coding. The specific needs to determine whether to capture is required, and it is not mandatory at compile-time. Common compile-time exceptions are IOException, the compiler will prompt, and you can capture them; common run-time exceptions include RuntimeException, NullPointerException, etc. Null pointers are usually caused by incomplete logical thinking and not processed, and will report errors in specific usage scenarios. .
   2. Exception handling operations
  1, try-catch
  

< div>

  try {
  // Business code
  //…
  Thread.sleep(1000L);
  } catch (Exception e) {
  // Ignore it
  }
  

   Try not to catch general exceptions like Exception, but should catch specific exceptions, here is the InterruptedException thrown by Thread.sleep().
   First, the code should be able to reflect more information more intuitively; second, we must ensure that the program does not catch exceptions that we do not want to catch, when When a code segment may have multiple exceptions, it should be exposed and handled instead of being caught unknowingly.
  

  try {
  // Business code
  //…
  } catch ( IOException e) {
  e.printStackTrace();
  }
  
   Try not to use e.printStackTrace(); output error messages. In larger systems or distributed systems, it is not easy to locate where the crop is output, and should be output to the corresponding log module.
  

  try-catch code segment will incur additional performance overhead, or put it another way, it often affects the JVM to optimize the code, so It is recommended to capture only the necessary code segment, and try not to cover the entire code segment with a big try
  2, throw and The difference between throws
  throw:
     acts in the method, and throws the specific object, and the exception is handled in the method

div>

  throws:

     acts on the declaration of the method, and the exception class is thrown by the caller of the method To deal with it
   Seeing 36 lectures on Yang Xiaofeng’s java core technology, it is equivalent to taking notes.

  1. The difference between Exception and Error

   Both Exception and Error inherit the Throwable class. In Java, only instances of the Throwable type can be thrown Throw or catch, which is the basic type of exception handling mechanism.

  

  Exception is an unexpected situation that can be expected during the normal operation of the program. It may and should be caught and dealt with accordingly.
  

   Error refers to a situation that is unlikely to occur under normal conditions. Most errors will cause the program (such as the JVM itself) to be in abnormal, Unrecoverable state. Since it is an abnormal situation, it is inconvenient and does not need to be captured.
  Exception is divided into checked exceptions and unchecked exceptions (ie compile-time exceptions and runtime exceptions). The checked exceptions must be in the source code Explicit capture processing is part of the compile-time check. Unchecked exceptions, that is, runtime exceptions, are usually logic errors that can be avoided by coding. The specific needs to determine whether to capture is required, and it is not mandatory at compile-time. Common compile-time exceptions are IOException, the compiler will prompt, and you can capture them; common run-time exceptions include RuntimeException, NullPointerException, etc. Null pointers are usually caused by incomplete logical thinking and not processed, and will report errors in specific usage scenarios. .
   2. Exception handling operations
  1, try-catch
  

< div>

  try {
  // Business code
  //…
  Thread.sleep(1000L);
  } catch (Exception e) {
  // Ignore it
  }
  

   Try not to catch general exceptions like Exception, but should catch specific exceptions, here is the InterruptedException thrown by Thread.sleep().
   First, the code should be able to reflect more information more intuitively; second, we must ensure that the program does not catch exceptions that we do not want to catch, when When a code segment may have multiple exceptions, it should be exposed and handled instead of being caught unknowingly.
  

  try {
  // Business code
  //…
  } catch ( IOException e) {
  e.printStackTrace();
  }
  
   Try not to use e.printStackTrace(); output error messages. In larger systems or distributed systems, it is not easy to locate where the crop is output, and should be output to the corresponding log module.
  

  try-catch code segment will incur additional performance overhead, or put it another way, it often affects the JVM to optimize the code, so It is recommended to capture only the necessary code segment, and try not to cover the entire code segment with a big try
  2, throw and The difference between throws
  throw:
     acts in the method, and throws the specific object, and the exception is handled in the method

div>

  throws:

     acts on the declaration of the method, and the exception class is thrown by the caller of the method To deal with it
   Seeing 36 lectures on Yang Xiaofeng’s java core technology, it is equivalent to taking notes

  Exception is a predictable unexpected situation during the normal operation of the program, which may and should be caught and handled accordingly.

  

   Error refers to a situation that is unlikely to occur under normal circumstances. Most errors will cause the program (such as the JVM itself) to be abnormal, Unrecoverable state. Since it is an abnormal situation, it is inconvenient and does not need to be captured.
  Exception is divided into checked exceptions and unchecked exceptions (ie compile-time exceptions and runtime exceptions). The checked exceptions must be in the source code Explicit capture processing is part of the compile-time check. Unchecked exceptions, that is, runtime exceptions, are usually logic errors that can be avoided by coding. The specific needs to determine whether to capture is required, and it is not mandatory at compile-time. Common compile-time exceptions are IOException, the compiler will prompt, and you can capture them; common run-time exceptions include RuntimeException, NullPointerException, etc. Null pointers are usually caused by incomplete logical thinking and not processed, and will report errors in specific usage scenarios. .
   2. Exception handling operations
  1, try-catch
  

< div>

  try {
  // Business code
  //…
  Thread.sleep(1000L);
  } catch (Exception e) {
  // Ignore it
  }
  

   Try not to catch general exceptions like Exception, but should catch specific exceptions, here is the InterruptedException thrown by Thread.sleep().
   First, the code should be able to reflect more information more intuitively; second, we must ensure that the program does not catch exceptions that we do not want to catch, when When a code segment may have multiple exceptions, it should be exposed and handled instead of being caught unknowingly.
  

  try {
  // Business code
  //…
  } catch ( IOException e) {
  e.printStackTrace();
  }
  
   Try not to use e.printStackTrace(); output error messages. In larger systems or distributed systems, it is not easy to locate where the crop is output, and should be output to the corresponding log module.
  

  try-catch code segment will incur additional performance overhead, or put it another way, it often affects the JVM to optimize the code, so It is recommended to capture only the necessary code segment, and try not to cover the entire code segment with a big try
  2, throw and The difference between throws
  throw:
     acts in the method, and throws the specific object, and the exception is handled in the method

div>

  throws: