Java basic questions: What are the similarities and differences between runtime exceptions and general exceptions? What is the difference between error and exception? Please write the five most common runtimeexception ?,
ThrowableIt is the parent class for Java Error handling. It has two subclasses: Error and Exception.
Error: Unexpected serious errors, causing the JVM virtual machine to be unable to continue executing, and almost cannot resume capturing
Exception: Recoverable. Java robust program means.
Java provides two main types of exceptions: runtime exception and checked exception (Exceptions checked during compilation ).
Checked exception(Exceptions checked during compilation): the JAVA compiler forces us to catch or throws these exceptions. Therefore, whether or not we want to handle such exceptions, we can only write a large number of catch blocks or throws to handle possible exceptions.
Such as IO exceptions and SQL exceptions.
Runtime exception: The program is compiled successfully, but it does not run properly. RuntimeException occurs, which is usually caused by a programmer error. The VM takes over will terminate the thread or main program. Such as incorrect type conversion, out-of-bounds array access, and access null pointers
The most common runtime exception
1. NullPointerException:
Int a1 [] = null;
System. out. print (a1 [2]);
2. ArrayIndexOutOfBoundsException
Int a [] = {2, 3, 5, 32, 6 };
For (int I = 0; I <6; I ++)
{
System. out. print (a [I]);
}
3. ClassCastException
Object I = new Integer (1 );
System. out. println (String) I );
4. ArithmeticException
Int a = 5/0;
5. NegativeArraySizeException
String [] s = new String [-10];