“This is the third day of my participation in the November Gwen Challenge. See details of the event: The last Gwen Challenge 2021”.

preface

  • Exceptions are finely broken down in Java. Most are divided into Exception and Error, both of which inherit from the Throwable class.
  • Exception is an error caused by a business during program execution. An Exception that the program itself can handle against Exception.
  • Errors are mostly problems that the program itself cannot resolve. For example, the memory is not enough, the stack is too deep and so on

What is an exception

  • An exception is something that cannot be determined at the code level during program execution. Depending on what the data produces

classification

  • Throwable is the parent class of exceptions, from which all exceptions are derived. There are two main exceptions + errors below it.

  • Error is a problem that can be detected at compile time. That’s what the JVM is handling

Unusual name role
NullPointerException Null pointer exception
ClassCastException Class conversion exception
IndexOutOfBoundsException Array out of bounds exception
IllegalArgumentException The parameter is invalid
ArrayStoreException An array of abnormal

NullPointerException

  • First, let’s summarize the circumstances in which this null-pointer exception occurs.
  • String variables are not initialized
  • ② The object is not initialized
  • To sum up, Null is an abstract object used in the Java world. Such an exception is a classic run-time exception, in that it is impossible to detect empty possibilities for the object at compile time. Exceptions occur only when specific data is passed during runtime

ClassCastException

  • The ClassCastException is a runtime exception thrown by the JVM when it detects an incompatible conversion between two types. Such errors usually terminate the user request. ClassCastException can occur when executing application code for any subsystem. A transformation instructs the Java compiler to treat a variable of a given type as another variable. Both base and user-defined types can be converted. The Java language specification defines the transformations that are allowed, most of which can be validated at compile time. However, some transformations also require runtime validation. If an incompatibility is detected during this runtime validation, the JVM throws a ClassCastException exception

IndexOutOfBoundsException

  • This exception is usually an index group subscript out-of-bounds exception. In Java, an array is a collection of fixed length. If you have an array of length 10, you want to get the eleventh element which is inherently impossible, so that’s what you’re going to getIndexOutOfBoundsException

IllegalArgumentException

  • Illegal parameters, in the process of program development, especially Java data format requirements and strict. Let’s say I define an integer argument, but you provide a floating-point argument that doesn’t meet my requirements. If we don’t have a checking mechanism at this point, we’re going to end up with dirty data. So adding a checking mechanism at this point helps a lot.

ArrayStoreException

  • As the name implies, an exception occurs when an array is stored.
Object[] obj = new String[3];

obj[0] = new Integer(0);
Copy the code
  • We define an array to take a String. But we add int data