Exception classification in Java, Java Classification

Source: Internet
Author: User

Exception classification in Java, Java Classification

The following describes several common exception types to help you identify program errors when an error occurs:

(1) Exception root classes of various exceptions

If you do not know where the Exception group will occur, you can define the Exception type as Exception when there is an Exception in this place.

(2) An error occurred while calculating the ArithmeticException.

This exception is often encountered during arithmetic operations. A common exception is thrown when the divisor is 0.

(3) ArrayIndexOfOutBoundsException array subscript out of bounds

When programming, remember that the Java array subscript starts from 0. the maximum value of the array subscript is 1 minus the array length, for example, arr [10]. The last array element is arr [9]. if it is written as arr [10], the ArrayIndexOfOutBoundsException exception will occur.

(4) NullPointException NULL pointer exception

This exception is thrown for objects that are not initialized or undefined.

(5) ClassNotFoundException cannot load the required class

After the java file is compiled, it may be because the. class file needs to be moved to another folder. When referencing this. class file, you must modify its path. Otherwise, a ClassNotFoundException exception will be thrown.

(6) The InputMismatchException data type does not match the required data type.

For example, if you need the int type, but the String type is obtained through the input box, an InputMismatchException will be thrown.

(7) The IllegalArgumentException method accepts invalid parameters.

This is because the parameter type in the input method is incorrect. For example, if the method plus (int a, int B) is called as a. plus (1, ),

IllegalArgumentException exception.

(8) An error occurred while converting the NumberFormatException string to a number.

An exception occurs when the data type is forcibly converted. If the string "123" is converted to the number 123, no exception occurs. However, if the string "1 @ 23" is converted to the int type, a ClassCastException is thrown.



These are common runtimeexceptions (runtime exceptions). That is to say, these exceptions do not need to be declared and thrown, or are caught forcibly.

 

 

Two examples

The JVM will throw the corresponding exception, such as the Code:
ArrayIndexOutOfBoundsException public void demo1 (){
Try {int a = 10/0 ;}
Catch (ArithmeticException AE)
{System. out. println ("Arithmetic Operation exception:" + AE. getMessage ());}
Catch (Exception e)
{System. out. println ("Other exceptions" + e. getMessage ());}}
Public void demo2 ()
{String strList [] = {"a", "B", "c"}; try {String str = strList [4]. toString ();}
Catch (ArrayIndexOutOfBoundsException AE)
{System. out. println ("array subscript out of bounds:" + AE. getMessage ());}
Catch (Exception e)
{System. out. println ("Other exceptions" + e. getMessage ());}}

In short, Exception is the parent class of all exceptions. If your Exception is caught by its subclass, it will not capture the Exception, such as the demo2 () method.
If ArrayIndexOutOfBoundsException Exception occurs, it will not be captured! So why capture multiple times?
Because ArrayIndexOutOfBoundsException is only an Exception in the array subscript out of bounds, it is more careful than Exception, and it can better explain the cause of the Exception!
If ArrayIndexOutOfBoundsException is not displayed, the Exception will be captured.


All exceptions are inherited from the java. lang. Throwable class.
Throwable has two direct subclasses: Error and Exception.
Exception
Exception allows you to use the class methods of any standard Java library, your own methods, and the base types thrown out of any exceptions during the runtime.
There are two types of Exceptions: RuntimeException and Checked Exceptions.
RuntimeException
RuntimeException is automatically handled by default. Therefore, you do not need to capture RuntimeException. However, in your own encapsulation, you may still need to throw a part of RuntimeException.
RuntimeException is a super class that may throw exceptions during the normal operation of the Java Virtual Machine. Any subclass of RuntimeException that may be thrown during method execution but not captured does not need to be declared in the throws clause.

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.