Exception
java异常捕获机制中的try-catch
try块是用来扩上可能出错的代码片段,catch块是用来捕获try块中代码抛出的错误并解决。
System.out.println("程序开始了");
try {
String str = "";
/*
* JVM在执行代码的过程中若发现了一个异常,则会实例化这种情况的异常实例,
* 并将代码整个执行过程完整设置到异常中来表示该错误报告,设置完毕后该异常抛出。
* 若抛出异常的这句代码有被try包围,则JVM会检查catch中是否有关注该异常,
* 关注则交给catch并解决,否则会将异常抛出到当前方法外,由调用当前方法的代码解决该异常。
*/
System.out.println(str.length());
System.out.println(str.charAt(0));
System.out.println(Integer.parseInt(str));
}catch(NullPointerException e) {
System.out.println("出现了空指针");
}catch(StringIndexOutOfBoundsException e) {
System.out.println("字符串下标越界了");
/*
* 应当养成一个好习惯,在最后一个catch中捕获Exception。
* 避免因未捕获异常导致程序中断。
*
* 当多个catch捕获不同异常时,这些异常间存在继承关系,
* 那么子类异常要在上先行捕获,父类异常在下。
*/
}catch(Exception e) {
System.out.println("反正就是出了个错");
}
System.out.println("程序结束了");
finally块
finally块定义在异常捕获机制的最后,可以直接跟在try块之后或者最后一个catch块之 后。
finally块中的代码一定执行,无论try块中的代码是否抛出异常。
所以通常会把释放资源等操作放在finally块中,例如关闭流。
System.out.println("程序开始了");
try {
String str = null;
System.out.println(str.length());
}catch(Exception e) {
System.out.println("出错了");
}finally {
System.out.println("finally中的代码执行了");
}
System.out.println("程序结束了");
finally对于流的处理
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream("src/day08/ExceptionDemo3.java")));
String line = null;
while((line = br.readLine())!=null) {
System.out.println(line);
}
}catch(Exception e) {
System.out.println("出错了");
}finally {
if(br!=null) {
try {
br.close();
} catch (IOException e) {
}
}
}
throw与throws
public class Person {
private int age;
public int getAge() {
return age;
}
/*
* 当一个方法中使用throw抛出一个异常时就要在方法上使用throws声明该类异常的抛出以通知调用者解决。
* 只有RuntimeException及其子类异常使用throw抛出时不强制要求必须使用throws声明。
* 其他异常都是强制要求的,否则编译不通过。
*/
public void setAge(int age)throws IllegalAgeException {
if(age<0||age>120) {
throw new IllegalAgeException("年龄不合法");
}
this.age = age;
}
}
/**
* 年龄不合法异常
*
* 自定义异常,通常是用来描述某个业务逻辑上出现的问题。
* 自定义异常的名字应当做到见名知义。
*/
public class IllegalAgeException extends Exception {
private static final long serialVersionUID = 1L;
public IllegalAgeException() {
super();
// TODO Auto-generated constructor stub
}
public IllegalAgeException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
public IllegalAgeException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public IllegalAgeException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public IllegalAgeException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
}
public static void main(String[] args) {
Person p = new Person();
/*
* 当调用一个含有throws声明异常抛出的方法时,编译器要求必须处理该异常。
* 处理手段有两种:
* 1:使用try-catch捕获并处理
* 2:在当前方法上继续使用throws声明该异常的抛出。
*/
try {
p.setAge(200);
} catch (IllegalAgeException e) {
e.printStackTrace();
}
System.out.println("年龄为:"+p.getAge());
}
重写父类一个含有throws异常抛出声明的方法时,子类该方法的throws的重写原则
public class ExceptionDemo5 {
public void dosome()throws IOException,AWTException{
}
}
class Son extends ExceptionDemo5{
//不再抛出任何异常
// public void dosome() {
//
// }
//可仅抛出父类方法中抛出的部分异常
// public void dosome throws IOException() {
//
// }
//允许抛出父类方法抛出异常的子类型异常
// public void dosome()throws FileNotFoundException{
//
// }
//不允许抛出额外异常
// public void dosome()throws SQLException{
//
// }
/*
* 不允许抛出父类方法抛出异常的父类型异常
*/
// public void dosome throws Exception() {
//
// }
}
Exception常用方法
public static void main(String[] args) {
System.out.println("程序开始了");
try {
String str = "abc";
System.out.println(Integer.parseInt(str));
}catch(Exception e) {
// e.printStackTrace();
System.out.println(e.getMessage());
}
System.out.println("程序结束了");
}