Java Learning note -6.java exception handling

Source: Internet
Author: User

First, Exception introduction

1. Exception: During the operation of the program, there are some unexpected situations that cause the program to exit.

2. An exception is represented by an object, and all exceptions are directly or indirectly inherited from the Throwable interface.

The Throwable interface is a class in the class library Java.lang package that derives two subclasses: Error and Exception

(1) The error class is mainly used to describe errors caused by errors or resource exhaustion in the Java Runtime system, which are generally handled by the system and do not need to be captured and processed by the general program itself.

(2) Exception class is divided into runtimeexception anomaly and non-runtimeexception anomaly

RuntimeException exception: is a program defect exception, is a design or implementation of the problem, such problems should be avoided and can not be captured, but in order to ensure that the program can still be executed after the error, in the development of the best use of try...catch processing

Non-runtimeexception exception: An exception caused by an external problem in the program, which must be handled syntactically, otherwise it cannot be compiled, called an exception.

3. Common exception classes (all inherited from the exception class, each of which represents a specific error)

System-defined Run exceptions System run error corresponding to exception
ClassNotFoundException The corresponding class was not found
ArrayIndexOutOfBoundsException Array out of bounds
FileNotFoundException The specified file or directory was not found
IOException Input, output error
Nullpointexception Referencing an empty object that has no memory space yet
ArithmeticException Arithmetic error
Interruptedexception Thread interrupted by other threads
Unknownhostexception Unable to determine IP address of host
SecurityException Security errors
Malformedurlexception URL format error

4. User-defined exception class (must inherit from Throwable or exception class (recommended))

Format:

class extends Exception {    public  myexception () {        Super();        ...    }      Public myexception (String s) {        Super(s);        ...    }      Public String toString () {        return("...");        ...    }}

Second, Java exception handling mechanism

1. Throwing and declaring exceptions: When a Java program throws a recognizable error at run time, it produces an object of the exception class corresponding to the error, which is called an exception throw

Throw exception Format:

Modifier return type method name (argument list) throws Exception class list {//Declaration exception

...

Throw exception class name; Throw exception

...

}

When the method throws an exception, the method cannot return to its caller, but into the exception-handling block

2. Catch exception: Accept the thrown exception object

Specific format:

Try {    // accept the monitored    block, the exception that occurs in this region // , and the catch is handled by the program specified  in the catch (Exception class name 1 exception formal parameter name    ) {// handle exception catch  (Exception class name 2 exception formal parameter name) {     // Handling Exceptions }... finally {    // final processing, code per execution }

Note: (1) Although exception captures the largest range, it can be processed directly with exception regardless of any anomalies, but multiple exceptions are best captured separately, rather than using exception to catch all exceptions directly

(2) When arranging multiple catch statement sequences for multiple exceptions, common exceptions should be placed in front

(3) Exceptions that trap small exceptions must be placed before an exception that captures a large range, or the program compiles an error

(4) The Finally statement provides a uniform exit for exception handling, regardless of whether an exception is thrown in the block specified by the try, or whether the exception type of the catch statement matches the type of exception being thrown, and the code specified by finally is executed, which provides a unified exit

(5) Cleanup of resources is usually possible in the finally statement, such as closing open files and closing data streams, etc.

Java Learning note -6.java exception handling

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.