Classification of exceptions in Java

Source: Internet
Author: User
Tags arithmetic throwable

Here are a few common types of exceptions that make it easy to check for errors when they occur:

(1) Exception the root class of various anomalies

You can define an exception type as exception if you do not know where the exception will occur and you can be certain that an exception will occur in this place.

(2) ArithmeticException arithmetic arithmetic error exception

This exception is often encountered in arithmetic operations, and the common case is the exception thrown when the divisor is 0.

(3) Arrayindexofoutboundsexception array subscript out of bounds

When programming, remember that the subscript for the Java array starts at 0, the array subscript maximum is reduced by 1, such as arr[10], the last array element is arr[9], and arrayindexofoutboundsexception exception occurs if written arr[10].

(4) nullpointexception null pointer exception

This exception is the exception that is thrown for calling an uninitialized or undefined object.

(5) ClassNotFoundException cannot load the required classes

After the Java file is compiled, it may be necessary to move the. class file to a different folder, at which point the. class file should be noted to modify its path, or the classnotfoundexception exception will be thrown.

(6) Inputmismatchexception input data type does not match the desired type of exception

For example, if you need an int type, but you get a string type from the input box, the inputmismatchexception exception will be thrown.

(7) IllegalArgumentException method accepts illegal parameters

This is an incorrect parameter type in the incoming method, such as method plus (int a,int b), which appears when written as A.plus (1,a).

IllegalArgumentException exception.

(8) NumberFormatException string converted to numeric exception

The exception that occurs when the data type is cast, the string "123" is converted to the number 123 without an exception, but the string "[email protected]" is converted to type int to throw a classcastexception exception.



These are common runtimeexception (run-time exceptions), meaning that these exceptions do not need to force the declaration to be thrown, or to force a catch exception.

Give two examples

When the program is out of the ordinary, the JVM throws a corresponding exception such as 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 exception" +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 exception" +e.getmessage ()); } }

In summary, exception is the parent class for all exceptions. If the exception that you appear is captured by his subclass, he will no longer capture such as the Demo2 () method
If there is a ArrayIndexOutOfBoundsException exception will not be captured! So why capture multiple times?
Because ArrayIndexOutOfBoundsException is just an exception to the array subscript, it is more careful than the exception and can explain the cause of the exception!
If it weren't for arrayindexoutofboundsexception, exception would have caught it.


All exceptions are inherited from the Java.lang.Throwable class.
Throwable has two direct subclasses, the error class, and the exception class.
Exception
Exception can make a base type that is thrown out of any standard Java library's class method, its own method, and any exception at run time.
Exceptions can be divided into execution exceptions (runtimeexception) and check exceptions (Checked Exceptions).
RuntimeException
RuntimeException are automatically processed by default. So you don't usually need to capture runtimeexception, but in your own package, you might still have to choose to throw a portion of the runtimeexception.
RuntimeException is a superclass of exceptions that might be thrown during the normal operation of a Java virtual machine. Any subclass of RuntimeException that may have been thrown but not captured during the execution of the method does not need to be declared in the throws clause.

Classification of exceptions in Java

Related Article

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.