zoukankan      html  css  js  c++  java
  • JAVA_SE基础——54.异常

    异常的体系:
    ----------| Throwable  所以异常或者错误类的超类
    --------------|Error  错误   错误一般是用于jvm或者是硬件引发的问题,所以我们一般不会通过代码去处理错误的。
    --------------|Exception 异常   是需要通过代码去处理的。


    如何区分错误与异常呢:
    如果程序出现了不正常的信息,如果不正常的信息的类名是以Error结尾的,那么肯定是一个错误。

    如果是以Exception结尾的,那么肯定就是一个异常。


    异常的处理:


    方式一:捕获处理(往下面还有方式二


     捕获处理的格式:

    try{
    可能发生异常的代码;


    }catch(捕获的异常类型 变量名){
    处理异常的代码....
    }


    捕获处理要注意的细节:
    1.
    code1中体现如果try块中代码出了异常经过了处理之后,那么try-catch块外面的代码可以正常执行。
    2.code2中体现如果try块中出了异常的代码,那么在try块中出现异常代码后面的代码是不会执行了。
    3.code3中体现一个try块后面是可以跟有多个catch块的,也就是一个try块可以捕获多种异常的类型。
    4.code4中体现一个try块可以捕获多种异常的类型,但是捕获的异常类型必须从小到大进行捕获,否则编译报错。



    code1

    package day10;
    
    
    public class TestException2 {
    	
    	public static void main(String[] args){
    		test(1,0);
    	}
    	public static void test(int a , int b){
    		try{
    			int c = a/b;
    		}catch (ArithmeticException e){
    			System.out.println("异常处理了  并返回当前异常对象的完整类名+病态信息 :"+e.toString());
    		}
    		System.out.println("try-catch块外面的代码可以正常执行中");
    	}
    }
    运行结果:

    异常处理了 回当前异常对象的完整类名+病态信息 :java.lang.ArithmeticException: / by zero
    try-catch块外面的代码可以正常执行中


    code2

    package day10;
    
    
    public class TestException2 {
    	
    	public static void main(String[] args){
    		test(1,0);
    	}
    	public static void test(int a , int b){
    		try{
    			int c = a/b;
    			System.out.println("看看是否被执行到~");
    		}catch (ArithmeticException e){
    			System.out.println("异常处理了  并返回当前异常对象的完整类名+病态信息 :"+e.toString());
    		}
    	}
    }
    运行结果:

    异常处理了  并返回回当前异常对象的完整类名+病态信息 :java.lang.ArithmeticException: / by zero

    ”看看是否被执行到~“  这句没有被执行到,因为int c = a/b;这句出现异常了,所以try块内的其余代码都不会执行,直接会被catch 处理掉。



    code3

    public class TestException2 {
    	
    	
    	public static void main(String[] args){
    		int[] arr = null;
    		test(10,2,arr);
    		
    	}
    	public static void test(int a , int b, int[] arr)  {
    		int c = 0 ;
    		try{
    			
    			c = a/b;
    			System.out.println(arr.length);
    		}catch (ArithmeticException e){
    			System.out.println("异常处理了  并返回回当前异常对象的完整类名+病态信息 :"+e.toString());
    		}catch (NullPointerException e){
    			System.out.println("出现了空指针异常....");
    		}catch (Exception e){
    			System.out.println("cc");
    		}
    		System.out.println("c="+c);
    	}
    }

    运行结果:

    出现了空指针异常....
    c=5


    code3这段代码要注意,try块能检测多种异常,但是只能处理一种异常,就是第一个异常。


    code4

    public class TestException2 {
    	
    	
    	public static void main(String[] args){
    		int[] arr = null;
    		test(10,2,arr);
    		
    	}
    	public static void test(int a , int b, int[] arr)  {
    		int c = 0 ;
    		try{
    			
    			c = a/b;
    			System.out.println(arr.length);
    		}catch (Exception e){
    			System.out.println("Exception类来捕获的话,其他类就捕获不了,相当于父类把工作都做完了,还有子类的事吗?");
    		}catch (NullPointerException e){
    			System.out.println("出现了空指针异常....");
    		}catch (ArithmeticException e){
    			System.out.println("cc");
    		}
    		System.out.println("c="+c);
    	}
    }

    运行结果:


    Exception  把   NullPointerException  和  ArithmeticException    的异常都捕获处理了,所以会提示错误,必须从小到大进行捕获,否则编译报错



    方式二:异常的处理方式----抛出处理



    抛出处理要注意的细节:
    1.code5中体现 如果一个方法的内部抛出了一个异常 对象,那么必须要在方法上声明抛出。
    2.code6中体现如果调用了一个声明抛出异常 的方法,那么调用者必须要处理异常。
    3.code7中体现如果一个方法内部抛出了一个异常对象,那么throw语句后面的代码都不会再执行了(一个方法遇到了throw关键字,该方法也会马上停止执行的)。
    4.code8中体现在一种情况下,只能抛出一种类型异常对象。(意思就是if())


    throw 与throws两个关键字:
    1. throw关键字是用于方法内部的,throws是用于方法声声明上的。
    2. throw关键字是用于方法内部抛出一个异常对象的,throws关键字是用于在方法声明上声明抛出异常类型的。
    3. throw关键字后面只能有一个异常对象,throws后面一次可以声明抛出多种类型的 异常


    概念

    ※Exception在程序中必须使用try...catch进行处理。

    ※RuntimeException可以不使用try...catch进行处理,但是如果有异常产生,则异常将由JVM进行处理(默认往上抛)



    code5

    public class TestException2 {
    	
    	
    	public static void main(String[] args){
    		try{
    			int[] arr = null;
    			test(10,2,arr);
    		}catch(NullPointerException e){
    			System.out.println("报告!  有异常出现");
    			e.printStackTrace();
    		}
    		
    	}
    	public static void test(int a , int b, int[] arr) throws ArithmeticException,NullPointerException{
    		int c = 0 ;
    		if(b==0){
    			throw new ArithmeticException(); //抛出一个异常对象...
    		}else if(arr==null){
    			throw new NullPointerException();
    		}
    			
    		c = a/b;
    		System.out.println(arr.length);
    		
    		System.out.println("c="+c);
    	}
    }

    运行结果:

    报告!  有异常出现
    java.lang.NullPointerException
    at day10.TestException2.test(TestException2.java:22)
    at day10.TestException2.main(TestException2.java:10)


    code6

    public class TestException2 {
    	
    	
    	public static void main(String[] args) 
    	{
    		//try{
    			int[] arr = null;
    			test(4,0,arr); //调用了一个 声明抛出异常类型 的方法
    		//}catch(Exception e){
    			System.out.println("报告! 异常出现了");
    		//	e.printStackTrace();
    		//}
    		
    	}
    	public static void test(int a , int b, int[] arr) throws Exception,NullPointerException{
    		int c = 0 ;
    		if(b==0){
    			throw new Exception(); //抛出一个异常对象...
    		}else if(arr==null){
    			throw new NullPointerException();
    		}
    			
    		c = a/b;
    		System.out.println(arr.length);
    		
    		System.out.println("c="+c);
    	}
    }	

    运行结果:



    P.S.

    这code6中的代码 ,一定要注意 在test方法抛出的如果是RuntimeException类的话 ,就是不捕获也不会提示报错的,因为异常分为检查性异常和运行时异常 检查性异常需要捕捉,运行时异常不会强制要求捕捉,也就是说非RuntimeException  包括Eception  都是需要捕获  



    code7

    public class TestException2 {
    	
    	
    	public static void main(String[] args) 
    	{
    		try{
    			int[] arr = null;
    			test(4,0,arr); //调用了一个 声明抛出异常类型 的方法
    		}catch(Exception e){
    			System.out.println("报告! 异常出现了");
    			e.printStackTrace();
    		}
    		
    	}
    	public static void test(int a , int b, int[] arr) throws Exception,NullPointerException{
    		int c = 0 ;
    		if(b==0){
    			throw new Exception(); //抛出一个异常对象...
    		}else if(arr==null){
    			throw new NullPointerException();
    		}
    			
    		c = a/b;
    		
    		System.out.println(arr.length);
    		
    		System.out.println("c="+c);
    	}
    }
    运行结果:

    报告! 异常出现了
    java.lang.Exception
    at day10.TestException2.test(TestException2.java:21)
    at day10.TestException2.main(TestException2.java:11)


    可见C的值没有打印出来,所以一抛出异常 后面的代码都不执行了。


    code8

    class Demo11 
    {
    	public static void main(String[] args) 
    	{
    		try{
    			int[] arr = null;
    			div(10,0,arr); //调用了一个 声明抛出异常类型 的方法
    		}catch(Exception e){
    			System.out.println("报告! 出现异常了");
    			e.printStackTrace();
    		}
    		
    	}
    
    
    	public static void div(int a, int b,int[] arr) throws Exception,NullPointerException {
    		if(b==0 || arr==null){
    			throw new Exception(); //抛出一个异常对象...
    			throw new NullPointerException();
    		}
    		int c = a/b;
    		System.out.println("c="+c);
    	}
    }
    运行结果:

    因为抛出了异常 就不会处理下面的语句了,方法都结束了 ,所以只能将2个异常分开成两种情况 才能避免编译时错误。

    if(b==0){
    	throw new Exception(); //抛出一个异常对象...
    }else if(arr==null){
    	throw new NullPointerException();
    }


    交流企鹅:654249738,和自学者交流群:517284938

  • 相关阅读:
    Eclipse程序员要掌握的常用快捷键
    HDU 1166——敌兵布阵——————【线段树单点增减、区间求和】
    nyoj1032——Save Princess——————【set应用】
    nyoj1087——摆格子——————【规律题】
    hrb——开锁魔法I——————【规律】
    ACdream 1099——瑶瑶的第K大——————【快排舍半,输入外挂】
    ACdream 1098——圆有点挤——————【数学计算】
    FZU 1884——排火车——————【栈的模拟】
    FZU 1921——栀子花开——————【线段树单点更新】
    FZU 1924——死锁——————【topo判环】
  • 原文地址:https://www.cnblogs.com/Jhaiha0/p/8465287.html
Copyright © 2011-2022 走看看