Java Exception handling mechanism

Source: Internet
Author: User
Tags exception handling throw exception

Exceptions are errors in a program, but not all errors are exceptions, and errors can sometimes be avoided. For example, if you use System.out.println (5/0), you are throwing a java.lang.ArithmeticException exception because you have a divisor with 0. Some exceptions need to be handled, while others do not require capture processing, which is described in detail below.

Days have the wind and cloud, people have unforeseen, Java program code is also so. In the programming process, you should first try to avoid errors and anomalies, for unavoidable, unpredictable circumstances should consider how to deal with the occurrence of anomalies.

In Java, the exception is represented by objects, Java processing of exceptions by the exception of the processing, different exceptions have different classifications, each of the corresponding type of exception (class), each exception corresponds to an exception (Class) object.

Where does the exception class come from? There are two sources, one is the basic exception type defined by the Java language itself, and the other is the exception that the user defines by inheriting the exception class or its subclasses. The Exception class and its subclasses are a form of throwable that indicates the conditions that a reasonable application wants to capture.

Where do the unusual objects come from? There are two sources, one is the Java Runtime Environment automatically throws system-generated exceptions, regardless of whether you are willing to capture and processing, it will always be thrown! Like an exception with a divisor of 0. The second is the exception that the programmer throws itself, this exception can be defined by the programmer itself, or it can be defined in the Java language and thrown with the throw keyword, which is often used to report some information about the exception to the caller.

Exceptions are for methods, and throwing, declaring, capturing, and handling exceptions are done in the method.

Java exception handling is managed through 5 keyword try, catch, throw, throws, finally. The basic procedure is to wrap the statement you want to monitor with a try statement block, and if an exception occurs inside a try statement block, the exception is thrown, your code catches the exception in the Catch statement block, and some of the system-generated exceptions are automatically thrown in the Java runtime. You can also declare the method to throw an exception by using the throws keyword, and then throw the exception object through throw inside the method. Speaking of here, probably many people do not know throws and throw difference, in fact very simple,

Throw is used to throw an exception in the method body. The syntax format is: Throw exception object. Throws is used to declare what exceptions the method might throw, after the method name, the syntax format is: throws exception type 1, exception type 2 ... Exception type N. As follows:

Java code

Import java.io.IOException;  
      
public class Throwsandthrow {public  
    void test (int a,int b) throws ioexception,arrayindexoutofboundsexception{  
        int c=a/b;  
        if (b==0) {  
            throw new ArithmeticException ();}}}  

A catch statement can have more than one exception that matches multiple exceptions, and when a catch statement block is executed, only the exception on the match is executed. If you don't understand the meaning of this sentence, you can run the following code:

Java code

Package A complete example of exception handling;  
      
Import java.io.*;  
      
public class TestException2 {public  
    static void Main (String args[]) {  
        int x,y,result;  
        x=5;  
        int a[]={0};  
        try{  
            inputstreamreader reader=new inputstreamreader (system.in);  
            BufferedReader input=new BufferedReader (reader);  
            SYSTEM.OUT.PRINTLN ("Input divisor:");  
            Reads the number  
            Y=integer.parseint (Input.readline ()) entered in a row  
            ; result=x/y;  
            a[10]=5;  
            SYSTEM.OUT.PRINTLN (result);  
            SYSTEM.OUT.PRINTLN ("statements that are not executed when an exception occurs");  
        catch (ArrayIndexOutOfBoundsException e) {//array  
            out of bounds exception System.out.println ("Handle array exceptions)";  
        } catch (ArithmeticException e) {  
            System.out.println ("Processing arithmetic exceptions");  
        } catch (Exception e) {  
            System.out.println ("Handling Exceptions");  
            return;}}  

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.