Simple Java Basics------Exceptions

Source: Internet
Author: User

I. Definition of an exception

An exception is an event that causes a program to run at run time because of a programming error or external factor. (It interrupts the normal order flow of the instruction)

Ii. Classification of anomalies

1.Error

Dynamic link errors, such as internal errors within the JVM system or resource exhaustion, are either unrecoverable or impossible to capture, resulting in an application outage.

2.Exception

Other general problems caused by programming errors or accidental external factors, when such anomalies are properly handled, the program has the opportunity to return to normal operation

① non-inspected (uncheched) exception

This is the error that occurs when the program is running, such as 1/0, null pointer exception, etc.

Error type conversion: Java.lang.ClassCastException

Array subscript out of bounds: java.langarraylndexoutofboundsexcetion

Null pointer access: java.lang.NullPointerException

Arithmetic exception (except 0 overflow): java.java.ArithmeticException

② inspected exception (cheched)

The class with the specified name was not found: java.lang.ClassNotFoundException

Access to files that do not exist: Java.io.FileNotFoundException

Exception occurred while manipulating the file: java.io.IOEception

Exception occurred while manipulating the database: Java.sql.SQLException

is the error in writing the Code times, and this error requires the programmer to deal with it in time.

Example: File not found exception

III. hierarchical structure of common anomalies and exception

}runtimeexception

? ArithmeticException : Mathematical Calculation exception

? NullPointerException : null pointer exception

? negativearraysizeexception : Negative group length exception

? arrayindexoutofboundsexception : Array index out of bounds exception

? classnotfoundexception : Class file not found exception

? classcastexception : Abnormal Shape

}IOException

? FileNotFoundException : File not found exception

? eofexception : Read-write file tail exception

? malformedurlexception : URL Format Error Exception

? socketexception : Socket Exception

? IOException

Throwable

/                              \

Error Exception

  Called errors, the virtual machine generates and throws, does not handle        /                \     

General Exception RuntimeException



Exception must be handled in the program

RuntimeException can be used without the use of Try...catch

But if there is an exception, the exception will be handled by the JVM

Acting



Iv. exception handling mechanism

If an exception occurs when executing the code, an exception class object is automatically generated, which is automatically committed to the JVM (a process known as throwing an exception) →JVM find out if there is a code exception that can handle this exception → find it and give it a handle (this process is called catch exception and handling exception) ; If unable to process, the program exits

V. Catching exceptions

Try{...} Catch{...} "Finally{...} 】

1.try Block

① capturing multiple exceptions can use 1 try Blocks

②try block can be placed in the exception code, you can also put no exception code

2.catch Block

① 1 Catch blocks can catch only one exception, and multiple catch blocks are used to capture when multiple exceptions occur

The method of the exception object appearing in the ②catch block

A.getmessage (): Print information about the exception object

B.printstacktrace (): Print error message, recommended use

③ If there is no exception, do not walk the catch block

④catch in block exception class order, subclass → Parent class Descending

3.finally

① whether or not the code has errors, go to the finally block

② role: To do resource cleanup work

③finally Block dispensable

First, throws key words

The method is declared with the throws keyword, and the method does not handle the exception, and the caller of the method handles

1. Function: Throw exception, do not handle

2. Declaration: After the method name

For example: public void Method () throws exception{...}

Second, throw and throws

Different points

Throw

Throws

Position

Method Internal

After method name

Role

Throw Exception Object

Throw Exception class

1.throw Use Note:

When throwing an exception object, the exception needs to be thrown by throws first

2.throws Use Note:

① when a method throws an exception, the object calling the method needs to handle the exception (who calls who handles it)

② when overriding a method. The anomalous range of what it does cannot be enlarged.

First, create a custom exception class

1. Create a class that inherits exception

2. Methods for copying exception classes

Ii. use of custom exception classes

With exception


Simple Java Basics------Exceptions

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.