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 ?,

Source: Internet
Author: User

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];

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.