Difference between java Error and Exception

created at 06-05-2022 views: 13

Difference between Error and Exception

Difference between Error and Exception

Both Error and Exception are subclasses of Throwable. In java, only instances of Throwable type can be thrown or caught, which is the basic type of exception handling mechanism.

  1. Exception and Error reflect the classification of different abnormal situations by the java platform designers. Exception is an unexpected situation that can be expected in the normal operation of the program, which can be caught and dealt with accordingly.
  2. Error refers to situations that are unlikely to occur under normal circumstances. Most Error will cause the program to be in an abnormal and unrecoverable state, which does not need to be captured. The common OutOfMemoryError is Error A subclass of `.
  3. Exception is divided into checkable exceptions (checked) and unchecked exceptions (unchecked). Checkable exceptions must be explicitly captured in the source code, which is part of the compile-time check and cannot be checked. Exceptions refer to runtime exceptions, such as NullPointerException, ArrayIndexOutOfBoundsException, etc., which are usually logical errors that can be avoided by coding. It is determined whether it needs to be caught according to needs, and it is not mandatory at compile time.

Java Exception Handling Keywords

  1. throw: Sometimes we explicitly want to create an exception object, and then throw it to stop the normal processing of the program. The throw keyword is used to throw an exception to the runtime to handle it.
  2. throws: When we throw any selected exception in a method without handling it, we need to use the throws keyword in the method signature to let the caller know about the exceptions that the method might throw. The caller method can handle these exceptions or propagate them to the caller method using the throws keyword. We can provide multiple exceptions in the throws clause and it can also be used with the main() method.
  3. try-catch: We use a try-catch block in our code to handle exceptions. try is the start of the block, catch handles exceptions at the end of the try block. We can use try to create multiple catch blocks, or nest try catch blocks. The catch block requires an argument of type Exception.
  4. The finally:finally block is optional and can only be used with a try-catch block. Since exceptions stop the execution process, we may open some resources that will not be closed, so we can use finally block. The finally block always executes regardless of whether an exception occurs.

Common Checked Exceptions and Unchecked Exceptions?

(1) Unchecked exception:

  • ArrayIndexOutOfBoundsException: Exception thrown when accessing an array with an invalid index *. If the index is negative or greater than or equal to the size of the array, the index is an illegal index
  • ArithmeticException: This exception is thrown when an abnormal operation condition occurs. (e.g. an instance of this class is thrown when an integer is "divided by zero")
  • IllegaArguementException: The exception thrown indicates that an invalid or incorrect parameter was passed to the method
  • NullPointerException: Null pointer exception (calling an instance method of a null object, etc.)
  • ClassCastException: Class cast exception
  • ArrayStoreException: Data storage exception, the type is inconsistent when operating the array

(2) Check exception

  • ClassNotFoundException: Could not find a definition for the class with the specified name
  • DataFormatException: Data format exception
  • IOException: Input and output exception
  • SQLException: exception that provides information about database access errors or other errors
  • FileNotFoundException: This exception is thrown when trying to open the file represented by the specified path name fails
  • EOFException: This exception is thrown when the end of the file or stream is unexpectedly reached during input
created at:06-05-2022
edited at: 06-05-2022: