java异常体系简介

1、异常体系:

在这里插入图片描述
图片来源:https://blog.csdn.net/zhanaolu4821/article/details/81012382

Trowable有两个子类:Error和Exception:
Error:
是与虚拟机有关的异常,如:系统崩溃,动态链接失败,虚拟机错误(AWTError、IOError),是不能捕获;
Jvm的内存消耗完会报的OutOfMemoryError,这种错误发生后,JVM就会停止线程;

Exception
包含RuntimeException和checkException异常
RuntimeException是运行时异常,主要原因是程序员没有正确的编写代码导致的异常,需要程序员修改代码,而不是对其进行捕获,
常见的RuntimeException有:IndexOfBoundlesException、ArthmaticException、NullException、NumberFormateException、classCastException
checkException异常是非RuntimeException就是checkedException,在代码编写阶段强制编程人员处理的异常,如FilenotFoundException
处理方式有两种: try catch 在方法声明中throws 抛出

2、try catch finally的执行顺序

情况1: try{} catch(){}finally{} return;

public class TryTest {

	public static void main(String[] args) {
          String result = trytestFunction();
          System.out.println(result);
	}

	private static String trytestFunction() {
        try {
			System.out.println("执行try方法!");
		} catch (Exception e) {
			System.out.println("执行catch方法!");
		}finally {
			System.out.println("执行finally方法!");
		}
		return "44444";
	}
}
执行try方法!
执行finally方法!
44444

情况2: try{ return; }catch(){} finally{} return;


public class TryTest {

	public static void main(String[] args) {
          String result = trytestFunction();
          System.out.println(result);
	}
   /**
             先执行try块中return 语句(包括return语句中的表达式运算),但不返回;
           执行finally语句中全部代码
            最后执行try中return 返回
    * 
    */
	private static String trytestFunction() {
        try {
			System.out.println("执行try方法!");
			return "try";
		} catch (Exception e) {
			System.out.println("执行catch方法!");
		}finally {
			System.out.println("执行finally方法!");
		}
		return "44444";
	}
}

执行try方法!
执行finally方法!
try

3、 try{ } catch(){return;} finally{} return;

  public class TryTest {

	public static void main(String[] args) {
          String result = trytestFunction();
          System.out.println(result);
	}
   /**
             有异常:
         执行catch中return语句,但不返回
          执行finally语句中全部代码,
      最后执行catch块中return返回。 finally块后的return语句不再执行
    * 无异常:
    *  正常顺序执行
    */
	private static String trytestFunction() {
        try {
        	throw new Exception("有异常!");
			/* System.out.println("执行try方法!"); */
			
		} catch (Exception e) {
			System.out.println("执行catch方法!");
			return "catch";
		}finally {
			System.out.println("执行finally方法!");
		}
	}
}
执行catch方法!
执行finally方法!
catch

4、情况4: try{ return; }catch(){} finally{return;}

public class TryTest {

	public static void main(String[] args) {
          String result = trytestFunction();
          System.out.println(result);
	}
   /**
            执行try块return语句(包括return语句中的表达式运算),但不返回;
        再执行finally块,
        执行finally块,有return,从这里返回。
    * 
    */
	private static String trytestFunction() {
        try {
			 System.out.println("执行try方法!");
			 return "try";
		} catch (Exception e) {
			System.out.println("执行catch方法!");
			return "catch";
		}finally {
			System.out.println("执行finally方法!");
			return "finally";
		}
	}
}
执行try方法!
执行finally方法!
finally

5、try{} catch(){return;}finally{return;}

public class TryTest {

	public static void main(String[] args) {
          String result = trytestFunction();
          System.out.println(result);
	}
   /**
      程序执行catch块中return语句(包括return语句中的表达式运算),但不返回;
再执行finally块,
执行finally块,有return,从这里返回。
    * 
    */
	private static String trytestFunction() {
        try {
			 System.out.println("执行try方法!");
			 throw new Exception("遇到错误!");
		} catch (Exception e) {
			System.out.println("执行catch方法!");
			return "catch";
		}finally {
			System.out.println("执行finally方法!");
			return "finally";
		}
	}
}
执行try方法!
执行catch方法!
执行finally方法!
finally

6、try{ return;}catch(){return;} finally{return;}

public class TryTest {

	public static void main(String[] args) {
          String result = trytestFunction();
          System.out.println(result);
	}
   /**
     有异常:

	  执行catch块中return语句(包括return语句中的表达式运算),但不返回;
	   再执行finally块
	   执行finally块,有return,从这里返回。
无异常:
	先执行try块
	再执行finally块
	
	执行finally块,有return,从这里返回。。
    * 
    */
	private static String trytestFunction() {
        try {
			 System.out.println("执行try方法!");
			 return "try";
		} catch (Exception e) {
			System.out.println("执行catch方法!");
			return "catch";
		}finally {
			System.out.println("执行finally方法!");
			return "finally";
		}
	}
}
执行try方法!
执行finally方法!
finally

总结:
1、任何执行try 或者catch中的return语句之后,在返回之前,如果finally存在的话,都会先执行finally语句;
2、如果finally中有return语句,那么程序就return了,所以finally中的return是一定会被return的;

3、常见的几种Exception

(此部分引用自https://blog.csdn.net/zhanaolu4821/article/details/81012382)
java.lang.IllegalAccessError:违法访问错误。当一个应用试图访问、修改某个类的域(Field)或者调用其方法,但是又违反域或方法的可见性声明,则抛出该异常。

java.lang.InstantiationError:实例化错误。当一个应用试图通过Java的new操作符构造一个抽象类或者接口时抛出该异常.

java.lang.OutOfMemoryError:内存不足错误。当可用内存不足以让Java虚拟机分配给一个对象时抛出该错误。
java.lang.StackOverflowError:堆栈溢出错误。当一个应用递归调用的层次太深而导致堆栈溢出或者陷入死循环时抛出该错误。

java.lang.ClassCastException:类造型异常。假设有类A和B(A不是B的父类或子类),O是A的实例,那么当强制将O构造为类B的实例时抛出该异常。该异常经常被称为强制类型转换异常。
java.lang.ClassNotFoundException:找不到类异常。当应用试图根据字符串形式的类名构造类,而在遍历CLASSPAH之后找不到对应名称的class文件时,抛出该异常。

java.lang.ArithmeticException:算术条件异常。譬如:整数除零等。
java.lang.ArrayIndexOutOfBoundsException:数组索引越界异常。当对数组的索引值为负数或大于等于数组大小时抛出。

java.lang.IndexOutOfBoundsException:索引越界异常。当访问某个序列的索引值小于0或大于等于序列大小时,抛出该异常。
java.lang.InstantiationException:实例化异常。当试图通过newInstance()方法创建某个类的实例,而该类是一个抽象类或接口时,抛出该异常。

java.lang.NoSuchFieldException:属性不存在异常。当访问某个类的不存在的属性时抛出该异常。
java.lang.NoSuchMethodException:方法不存在异常。当访问某个类的不存在的方法时抛出该异常。
java.lang.NullPointerException:空指针异常。当应用试图在要求使用对象的地方使用了null时,抛出该异常。譬如:调用null对象的实例方法、访问null对象的属性、计算null对象的长度、使用throw语句抛出null等等。
java.lang.NumberFormatException:数字格式异常。当试图将一个String转换为指定的数字类型,而该字符串确不满足数字类型要求的格式时,抛出该异常。

java.lang.StringIndexOutOfBoundsException:字符串索引越界异常。当使用索引值访问某个字符串中的字符,而该索引值小于0或大于等于序列大小时,抛出该异常。

发布了15 篇原创文章 · 获赞 7 · 访问量 2855

猜你喜欢

转载自blog.csdn.net/xiaocaodeshengri/article/details/100761821
今日推荐