Java foundation sixteenth day summary - Exception Handling

table of Contents:

I. Overview and abnormal abnormal architecture

Second, common exceptions

Third, an exception handling mechanism: try-catch-finally

Fourth, exception handling mechanism two: throws

V. manually throw an exception: throw

VI: User-defined exception classes

 

/*---------------split line-----------------*/

I. Overview and abnormal abnormal architecture

A lot of problems not by code to avoid: such as customer input format, whether to read the file exists, whether the network remains open.

Syntax and logic errors are not an exception.

Unusual event during the execution of Java programs that occur can be divided into two categories:

Error: Java Virtual Machine serious problem can not be solved. Such as: internal JVM system errors, resource depletion and other serious conditions. For example StackOverflowError and OOM. Generally do not write the code targeted for processing.

Exception: other general issues should accidental programming errors or external factors, you can use the code targeted for processing. E.g:

> Null pointer access

> Trying to read a file that does not exist

> Network connection is interrupted

> Array subscript out of bounds

Category: compile-time and run-time anomaly anomalies

 

 

 

 

1. runtime exception

Typically programmed logic errors, java. Abnormal lang.RuntimeException class and its subclasses are running.

For these exceptions, it can not handle, more common. If all of the processing, will affect the readability and efficiency of the program.

 

2. Compile abnormal

It means the compiler requires that exceptions must be disposed of, namely the general program at run-time exception due to external factors. Abnormal compiler requires Java program must declare all capture or compiler.

Such anomalies, if not addressed, could lead to unexpected results.

 

Second, common exceptions

 

 Third, an exception handling mechanism, try-catch-finally

Java exception handling mechanism adopted, the program code is exception handling together, separate from the normal program code, makes the program simple, elegant, easy to maintain.

Method 1: try-catch-finally

Second way: throws + Exception Type

Java provides is caught throwing exception handling model.

 

 Catch exceptions: a method throws an exception, the exception object is thrown to the caller method of treatment. If the exception is not handled in the caller method, we continued to throw the upper caller of the method. Until the exception is handled.

This process is called catch the exception.

如果一个异常回到main(),并且main()也不处理,则终止程序。

程序员通常只能处理Exception,而对Error无能为力。

 

 try

补货异常的第一步是用try{...}语句块选定补货异常的范围,将可能出现异常的代码放在try语句块中。

catch(Exceptiontype e)

catch语句块中是对异常对象进行处理的代码。每个try语句块可以伴随一个或多个catch语句,用于处理可能产生的不同类型的异常对象。

如果明确知道产生的是何种异常,可以用该异常类作为catch的参数;也可以用其父类作为catch的参数。

 

捕获异常的有关信息:

与其他对象一样,可以访问一个异常对象的成员变量或调用它的方法。

> getMessage() 获取异常信息,返回字符串

> printStackTrace()获取异常类名或异常信息,以及异常出现在程序中的位置。返回值viod。

 

finally

> 捕获异常的最后一步是通过finally语句为异常处理提供一个出口,使得在孔志流转到程序的其他部分以前,能够对程序的状态作统一的管理。

> 不论在try代码块中是否发生了异常事件,catch语句是否执行,catch语句是否有异常,catch语句中是否有return,finally块中的语句都会被执行。

> finally语句和catch语句是任选的

 

RuntimeException类或是它的子类异常Java自己也能捕获,并且编译通过,但运行时会发生异常使得程序终止。

如果抛出的异常是IOException等类型的非运行时异常,则必须捕获,否则编译错误。也就是说,必须处理编译时异常,将异常捕捉,转化为运行时异常。

 

四、异常处理机制二:throws

声明抛出异常是Java中处理异常的第二种方式,一个方法中的语句执行时可能出现的某种异常,但不能确定如何处理这种异常,则此方法应显示的声明抛出异常,表明方法将不对这些异常进行处理,而由该方法的调用者负责处理。

 

重写方法声明抛出异常的原则

重写方法不能抛出比被重写方法范围更大的异常类型。在多态的情况下,对metthodA()方法的调用-异常的捕获按父类声明的异常处理。

 

 五、手动抛出异常

Java异常类对象除在程序执行过程中出现异常时由系统自动生成并抛出。

首先要生成异常类对象,然后通过throw语句实现抛出操作(提交给Java运行环境)。

IOException e = new IOException();

throw e;

可以抛出的异常必须是Throwable或其他子类的实例。下面的语句在编译时将会产生语法错误。

throw new String("want to throw");

六、用户自定义异常类

一般,用户自定义异常类都是RuntimeException的子类

自定义异常通常需要编写几个重载的构造器

自定义异常需要提供serialVersionUID

自定义异常通过throw抛出

自定义异常最重要的是异常类的名字,当异常出现时,可以根据名字判断异常类型。

用户自定义异常类MyException,用于描述数据取值范围错误信息。用户自己的异常类必须继承现有的异常类。

 

总结:异常处理5个关键字

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/zhoutie170821/p/11968692.html