Java Exception classification

Source: Internet
Author: User
Tags throwable

Go Java Exception classification

First, the basic concept

Look at the exception structure diagram of Java

Throwable is the root of all anomalies, java.lang.Throwable
Error is wrong, java.lang.Error
Exception is abnormal, java.lang.Exception

Second, Exception

Generally divided into checked and runtime exceptions, all RuntimeException classes and their subclasses are known as runtime exceptions, exceptions that do not belong to this category are called Checkedexception.


①checked exception

Only the Java language provides checked exceptions, and Java considers that checked exceptions are exceptions that can be handled, so the Java program must display the handling of the checked exception. If the program does not handle the checked exception, the program will compile with an error that cannot be compiled. This embodies the design philosophy of Java: Code without perfect error handling has no chance of being executed at all. There are two types of checked exception handling methods

1 The current method knows how to handle the exception, and the Try...catch block is used to handle the exception.
2 The current method does not know how to handle the exception that is thrown when the method being defined is declared.

Package cn.xy.test;    Import java.io.IOException; /** * Checked Exception test method * @author XY * * */public class Checkedexceptionmethods {//Total exception class, both Checkedexception and Runtim Eexception, so one of the checkedexception must handle the public void Method1 () throws Exception {System.out.println ("I'm throwing an XOR      Methods of the general category ");          }//Capture and handle this exception public void testmethod1_01 () {try {method1 ();          } catch (Exception e) {e.printstacktrace ();      }}//pass exception to public void Testmethod1_02 () throws Exception {method1 ();      } public void Testmethod1_03 () throws Exception {throw new Exception ();          } public void Testmethod1_04 () {try {throw new Exception ();          } catch (Exception e) {e.printstacktrace (); }}//Checkedexception typical representative IOException public void method2 () throws IOException {System.out.println ("I am the method of throwing IO exceptions");          } public void testmethod2_01 () {try {method2 ();          } catch (Exception e) {e.printstacktrace ();      }} public void Testmethod2_02 () throws Exception {method2 ();   }    }

We are more familiar with the checked anomalies have

Java.lang.ClassNotFoundException
Java.lang.NoSuchMetodException
Java.io.IOException

②runtimeexception

Runtime such as divisor is 0 and set subscript out of bounds, and its frequent, processing trouble, if the display declaration or capture will have a significant impact on the readability and operational efficiency of the program. So the system automatically detects them and gives them to the default exception handlers. Of course, if you have processing requirements, you can also show them capture.

Package cn.xy.test;    /**  * Run-time Exception test method  * @author XY * * */Public  class runtimeexcetionmethods  {public      void Method3 () throws RuntimeException      {          System.out.println ("I am the method of throwing runtime exceptions");      }        public void testmethod3_01 ()      {          method3 ();      }        public void testmethod1_02 ()      {          throw new runtimeexception ();      }  }  

The subclasses of the Rumtimeexception class that we are more familiar with are

Java.lang.ArithmeticException
Java.lang.ArrayStoreExcetpion
Java.lang.ClassCastException
Java.lang.IndexOutOfBoundsException
Java.lang.NullPointerException

Third, Error

When a program has an uncontrolled error, it is common practice to notify the user and abort the execution of the program. Unlike exceptions, an object of error and its subclasses should not be thrown.

Error is a subclass of Throwable that represents a compile time and system error that indicates a serious problem that a reasonable application should not attempt to capture.

The error is generated and thrown by the Java virtual machine, including dynamic link failures, virtual machine errors, and so on. The program does not handle it.

Java Exception classification

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.