Java exception Classification

Source: Internet
Author: User

I. Basic Concepts
View the exception structure of java
 
 
Throwable is the root of all exceptions. java. lang. Throwable
Error is an Error. java. lang. Error
Exception is an Exception. java. lang. Exception
 

Ii. Exception
It is generally divided into Checked exceptions and Runtime exceptions. All RuntimeException classes and their subclass instances are called Runtime exceptions. exceptions that do not belong to this category are called CheckedException.

① Checked exception
Only the java language provides the Checked exception. Java considers the Checked exception to be an exception that can be handled. Therefore, the Java program must display and handle the Checked exception. If the program does not handle the Checked exception, the program will encounter an error during compilation and cannot be compiled. This embodies the design philosophy of Java: code without perfect error handling has no chance to be executed. There are two methods to handle Checked exceptions
1. If the current method knows how to handle the exception, use try... catch Block to handle the exception.
2. If the current method does not know how to handle the exception, this exception is thrown when the method is defined as a declaration.
 
Package cn. xy. test;
 
Import java. io. IOException;
 
/**
* Checked exception Test Method
* @ Author xy
*
*/
Public class CheckedExceptionMethods
{
// Total exception classes, including checkedException and RuntimeException. Therefore, the checkedException must be handled.
Public void method1 () throws Exception
{Www.2cto.com
System. out. println ("I am the method that throws the exception class ");
}
 
// Capture and handle this exception
Public void testmethod000001 ()
{
Try
{
Method1 ();
}
Catch (Exception e)
{
E. printStackTrace ();
}
}
 
// Pass the exception
Public void testmethod000002 () throws Exception
{
Method1 ();
}
 
Public void testmethod000003 () throws Exception
{
Throw new Exception ();
}
 
Public void testmethod000004 ()
{
Try
{
Throw new Exception ();
}
Catch (Exception e)
{
E. printStackTrace ();
}
}
 
// CheckedException typical IOException
Public void method2 () throws IOException
{
System. out. println ("I am throwing an IO exception ");
}
 
Public void testMethod2_01 ()
{
Try
{
Method2 ();
}
Catch (Exception e)
{
E. printStackTrace ();
}
}
 
Public void testMethod2_02 () throws Exception
{
Method2 ();
}
 
}
The common Checked exceptions include:
Java. lang. ClassNotFoundException
Java. lang. NoSuchMetodException
Java. io. IOException
 
② RuntimeException
Runtime, for example, if the divisor is 0 and the array subscript is out of bounds, is frequently generated and difficult to handle. displaying a declaration or capture will greatly affect the readability and Running Efficiency of the program. Therefore, the system automatically detects and sends them to the default exception handling program. If you have processing requirements, you can also capture them.
 
Package cn. xy. test;
 
/**
* Running exception Test Method
* @ Author xy
*
*/
Public class RuntimeExcetionMethods
{
Public void method3 () throws RuntimeException
{
System. out. println ("I am throwing a running exception ");
}
 
Public void testMethod3_01 ()
{
Method3 ();
}
 
Public void testmethod000002 ()
{
Throw new RuntimeException ();
}
}
The RumtimeException classes we are familiar with include
Java. lang. ArithmeticException
Java. lang. ArrayStoreExcetpion
Java. lang. ClassCastException
Java. lang. IndexOutOfBoundsException
Java. lang. NullPointerException

Iii. Error
When an uncontrollable error occurs in a program, it is usually used to notify the user and stop the program execution. Different from exceptions, the objects of errors and Their subclasses should not be thrown.
An Error is a subclass of throwable, representing the Compilation Time and system errors. It is used to indicate serious problems that a reasonable application should not try to capture.
Errors are generated and thrown by the Java Virtual Machine, including dynamic link failures and Virtual Machine errors. The program does not process it.

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.