Java Exception Handling analysis (Part 1)

Source: Internet
Author: User

Java Exception Handling analysis (Part 1)
An exception

Exceptions are some errors in the program, but not all errors are exceptions, and sometimes they can be avoided. For example, you have less code

A semicolon (;) indicates that java. lang. Error is returned. If you use System. out. println (11/0 ),

If the divisor is set to 0, a java. lang. ArithmeticException exception is thrown. Java exceptions are a mechanism provided by Java to handle errors in programs.

There are many causes of exceptions, usually including the following categories:

User a has entered illegal data.

B. the file to be opened does not exist.

Connection interruption or JVM memory overflow during network C communication.

These exceptions are caused by user errors, program errors, and physical errors.

 

To understand how Java Exception Handling works, you need to master the following three types of exceptions:

1) Check exception: The most common check exception is caused by a user error or problem, which cannot be foreseen by the programmer. For example

If a file does not exist, an exception occurs. These exceptions cannot be simply ignored during compilation.

2) runtime exception: a runtime exception may be avoided by programmers. In contrast to checking exceptions, runtime exceptions can be ignored during compilation.

3) error: the error is not an exception, but out of programmer control. Errors are usually ignored in the code. For example, an error occurs when the stack overflows.

This happens, and they cannot be checked during compilation.

 

When an exception occurs during Java program execution, an exception class object can be generated, which encapsulates the information of the exception event and extracts it

To the Java runtime system. This process is called throwing an exception.

When the system receives an exception object during Java runtime, it looks for code that can handle this exception and submits it to the system for processing. This process is called

To capture exceptions.

Level 2 of the Exception class

All Exception classes are subclasses inherited from the java. lang. Exception class. The Exception class is a subclass of the Throwable class. Except for the Exception class,

Throwable also has a subclass Error. Java programs usually do not capture errors. Errors generally occur in the case of serious faults, they are handled in the Java program

Domain name. Error indicates an Error in the runtime environment. For example, JVM memory overflow. Generally, the program will not be restored from the error.

The exception class has two main subclasses: The IOException class and the RuntimeException class.

In Java built-in classes (as described later), most common and non-checkable exceptions occur.

Java built-in exception classes

The Java language defines some exception classes in the java. lang standard package. The subclass of the standard runtime exception class is the most common exception class. The java. lang Package

Is loaded to all Java programs by default, so most exceptions inherited from the exception class at runtime can be directly used.

Java also defines some other exceptions based on each class library. The following table lists non-checkable exceptions in Java.

 

 

The following table lists the checked exception classes defined by Java in the java. lang package.

Four exception methods

The following lists the main methods of the Throwable class:

5. Capture exceptions

You can use the try and catch keywords to catch exceptions. Try/catch code blocks are placed where exceptions may occur. The code in the try/catch code block is called protection

The try/catch syntax is as follows:

 

Try {// program code} catch (ExceptionName e) {// catch Block}

 

The Catch statement contains the Declaration to capture the exception type. When an exception occurs in the protection code block, the catch block after try will be checked. If

The generated exception is contained in the catch block. The exception is passed to the catch block, which is the same as passing a parameter to the method. If the try block does throw an exception

Usually, terminate the program execution first, and the control of the program will be handed over to the exception handling program in the catch Block.

Example: The following example declares an array with two elements. An exception is thrown when the Code tries to access the third element of the array.

Test. java source file code:

 

import java.io.*;public class Test{public static void main(String[] args){try{int a[] = new int[2];                        System.out.println("Access element three :" + a[3]);                }catch(ArrayIndexOutOfBoundsException e){ System.out.println("Exception thrown  :" + e);                }                System.out.println("Out of the block");}}

 

The output result of the above code compilation and running is as follows:

The exception thrown above is the array out-of-bounds exception caused by number 3.

Six multiple capture Blocks

A try code block is followed by multiple catch code blocks. In multiple catch statements, the writing sequence of the exception type must be sub-

The class is in front and the parent class is in the back.

The syntax of multiple capture blocks is as follows:

 

Try {// program code} catch (exception type 1 Abnormal variable name 1) {// program code} catch (exception type 2 abnormal variable name 2) {// program code} catch (exception type 2 abnormal variable name 2) {// program code}

 

The above code segment contains three catch blocks. You can add any number of catch blocks after the ry statement. If an exception occurs in the protection code, the exception is

To the first catch Block. If the data type of the thrown exception matches predictiontype1, it will be captured here; if it does not match, it will be

To the second catch Block. Until the exception is caught or all catch blocks are passed.

Example: shows how to use multiple try/catch methods.

 

try{    file = new FileInputStream(fileName);    x = (byte) file.read();}catch(IOException i){       i.printStackTrace();    return -1;}catch(FileNotFoundException f){    f.printStackTrace();    return -1;}

 

 

 

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.