Java Exception
An exception is an event,which occurs during the execution of a program,that disrupts the normal flow of the program’s instructions–Oracle Java doc
当方法中发生错误,方法创建一个对象并把它传递给运行时系统。对象被称为异常对象;创建一个异常对象并传递给运行时系统叫做抛出异常。
Call Stack
Three Kind of Exception
checked exception
all exceptions are checked exceptions,except for :Error,RuntimeException and their subclasses
error
application can not anticipate(预见)or recover from;Errors are not subject to the catch or specify requirement.
runtime exception
indicate programming bugs.
catching and handling Exceptions
try block
try{
//code
}
catch block
try{
}catch(ExceptionType1 e1){
}catch(ExceptionType2 e2){
}
catch more than one one type of exception with one Exception Handler
use ‘|’
catch(IOException|SQLException ex){
logger.log(ex);
throw ex;
}
try-with-resources statement
package tij.javaexception;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.zip.ZipFile;
/**
* Created by Administrator on 2015/11/7.
*/
public class TryWithResourse {
//如果readline和close方法同时抛出异常
//readFirstLineFromFileWithFinallyBlock方法将抛出finally块中的异常
//try块中抛出的异常将被抑制
static String readFirstLineFromFileWithFinallyBlock(String path)
throws IOException {
BufferedReader br = new BufferedReader(new FileReader(path));
try {
return br.readLine();
} finally {
if (br != null) br.close();
}
}
//如果try块和try-with-resources都抛出异常
//那么readFirstLineFromFile方法将抛出try块中的异常
//try-with-resources中的异常将被抑止
static String readFirstLineFromFile(String path) throws IOException {
//try-with-resources statement
//br会被close不管try语句是正常的完成了还是突然的完成了.
try(BufferedReader br=
new BufferedReader(new FileReader(path))
){
return br.readLine();
}
}
/*
try-with-resources语句可以抛出异常
在下面这个方法中try块中抛出一个异常,try-with-resources中抛出俩异常
当try块中抛出一个异常,try-with-resources中抛出一到多个异常的时候
try-with-resources中抛出的异常被抑止
try块中的异常被方法writeToFileZipFileContent抛出
被抑止的异常可以通过调用try块抛出的异常的Throwable.getSuppressed方法提取出来
*/
public static void writeToFileZipFileContent(String zipFileName,String outputFileName) throws IOException{
Charset charset= StandardCharsets.UTF_8;
Path path= Paths.get(outputFileName);
//打开zip文件,并创建输出文件用try-with-resource语句
//当直接跟随的代码块终止时,不管是因为正常执行还是出现异常
//zipFile,writer对象的close方法都被按顺序自动调用
//close方法的调用顺序与创建对象的顺序相反.
try(
ZipFile zipFile=new ZipFile(zipFileName);
BufferedWriter writer= Files.newBufferedWriter(path,charset);
){
}
}
//try-with-resources 语句可以跟catch和finally,但是它们是在声明的资源被关闭后执行的.
}
How to throw Exceptions
all methods use throw statement to throw an Exception
throw someThrowableObject;//Throwable 类的子类
Throwable class and It’s Subclasses
Error class
当动态链接失败或者虚拟机硬件错误出现,虚拟机抛出Error
Exception class
一个Exception类表明一个问题出现了,但不是严重的系统问题
RuntimeException表明对API的错误使用
Chained Exceptions
Chained Exceptions
总结
程序可以用异常来表明一个错误的出现。用throw来抛出异常并提供一个异常对象-Throwable类的子类对象。一个抛出未捕获的受检查异常的方法必须在方法声明中包括一个throws子句。
- try语句块用来识别一块会出现异常的代码
- catch块用来异常处理,处理特定的异常
- finally语句块保证执行,用来关闭文件,恢复资源除此之外在try中代码之后进行清理。