Java程序在执行过程中所发生的异常事件可分为两类:
Error: Java虚拟机无法解决的严重问题。如:JVM系统内部错误、资源耗尽等严重情况。一般不编写针对性的代码进行处理。
Exception: 其它因编程错误或偶然的外在因素导致的一般性问题,可以使用针对性的代码进行处理。例如:
- 空指针访问
- 试图读取不存在的文件
- 网络连接中断
分类:编译时异常和运行时异常
常见异常
java.lang.RuntimeException
- ClassCastException
- ArrayIndexOutOfBoundsException
- NullPointerException
- ArithmeticException
- 。。。
java.io.IOExeption
- FileNotFoundException
- EOFException
java.lang.ClassNotFoundException
java.lang.InterruptedException
java.io.FileNotFoundException
java.sql.SQLException
异常使用
1.运用Java异常处理机制
try…catch语句
finally语句:任何情况下都必须执行的代码
throws子句:声明可能会出现的异常
throw语句:抛出异常
2.异常处理的原理
1.java虚拟机用方法调用栈来跟踪每个线程中一系列的方法调用过程
2.如果在执行方法的过程中抛出异常,则Java虚拟机必须找到能捕获改异常的catch代码块
3.当Java虚拟机追溯到调用栈的底部的方法时,如果仍然没有找到处理改异常的代码块
3.异常流程的运行过程
finally语句不被执行的唯一情况就是先执行了用于终止程序的system.exit()方法
return用于退出本方法
finally代码块虽然在return语句之前执行
建议不要在finally代码块中使用return语句
捕获异常有关信息
与其它对象一样,可以访问一个异常对象的成员变量或调用它的方法。
- getMessage() 获取异常信息,返回字符串
- printStackTrace() 获取异常类名和异常信息,以及异常出现在程序中的位置。返回值void。
异常表达的第一种方式
public int method1(){
try {
//System.out.println();
return 1;
}catch (Exception e){
e.printStackTrace();
return 2;
}finally {
System.out.println("你好,Android");
return 3;
}
}
结果:
你好,Android
3
首先执行try没有遇到异常先执行finally,finally有返回值,则结束
修改代码
public int method1(){
try {
System.out.println(10/0);
return 1;
}catch (Exception e){
e.printStackTrace();
return 2;
}finally {
System.out.println("你好,Android");
return 3;
}
}
结果:
java.lang.ArithmeticException: / by zero
你好,Android
3
异常表达第二种方式:声明抛出异常(throws)
public void test1() {
try {
method2();
}catch (FileNotFoundException e){
System.out.println(e.getMessage());
}catch (IOException e){
e.printStackTrace();
}
method3();
}
public void method3(){
try {
method1();
}catch (FileNotFoundException e){
System.out.println(e.getMessage());
}catch (IOException e){
e.printStackTrace();
}
}
public void method2() throws FileNotFoundException, IOException {
method1();
}
public void method1() throws FileNotFoundException,IOException {
FileInputStream inputStream = new FileInputStream("hello.txt");
int b;
while ((b = inputStream.read()) != -1) {
System.out.println(b);
}
inputStream.close();
}
当在此方法内部出现异常的情况的时候,会抛出一个异常类的对象,抛给方法的调用者。
异常的方法可以逐层向上抛出,直至main中。当然向上抛的过程中。可以通过try…catch-finally进行捕获
java异常处理:抓抛模型
- 1.抓:异常的处理,有两种方式(try-catch-finally和throws+异常类型)
- 2.抛:一旦执行过程中,出现异常会抛出一个异常类的对象(自动抛出VS手动抛出(throw+异常的对象))
手动抛出异常
public void test2() {
Circle circle=new Circle(5);
Circle circle1=new Circle(4);
System.out.println(circle.compareTo(circle1));
System.out.println(circle.compareTo(new String()));
}
class Circle {
double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public int compareTo(Object o){
if(o==this){
return 0;
}else if(o instanceof Circle){
Circle circle= (Circle) o;
if(circle.radius<this.radius){
return 1;
}else if(circle.radius==this.radius){
return 0;
}else{
return -1;
}
}else{
throw new RuntimeException("两个不是一个东西");
}
}
}
自定义异常
第一种方式
public class MyException extends RuntimeException {
static final long serialVersionUID = -703489719074576639L;
public MyException() {
}
public MyException(String msg) {
super(msg);
}
}
第二种方式
public class MyException2 extends Exception {
static final long serialVersionUID = -703489719074576639L;
public MyException2() {
}
public MyException2(String msg) {
super(msg);
}
}