zoukankan      html  css  js  c++  java
  • Java 异常的捕获及处理

    1、异常的产生以及对程序的影响

    10/0 异常产生后,打印出异常信息,中断程序的继续执行。

    我们要做的就是即使异常也要让程序继续正确的执行下去。

    2、异常处理的格式

    try{
        //有可能出现异常的代码
    }[catch(异常类型 对象){
        //异常处理
    } catch(异常类型 对象){
        //异常处理
    } catch(异常类型 对象){
        //异常处理
    }…] [finally]{
        //不管是否出现异常,都会同一执行的代码
    }

    组合try…catch 、try…catch…finally、try…finally

    public class TestException 
    {
    	public static void main(String[] args) 
    	{
    		System.out.println("除法开始。。。");
    		try 
    		{
    			System.out.println("除法开始进行:"+10/0);
    		}catch(ArithmeticException e){
    			System.out.println("******出现异常了*****");
    		}
    		System.out.println("除法结束。。。");
    	}
    }
    
    除法开始。。。
    ******出现异常了*****
    除法结束。。。

    下面可以打印出异常的信息。(try…catch)

    package exception;
    
    public class TestException 
    {
    	public static void main(String[] args) 
    	{
    		System.out.println("除法开始。。。");
    		try 
    		{
    			System.out.println("除法开始进行:"+10/0);
    		}catch(ArithmeticException e){
    			e.printStackTrace();
    		}
    		System.out.println("除法结束。。。");
    	}
    }
    
    除法开始。。。
    java.lang.ArithmeticException: / by zero
    	at exception.TestException.main(TestException.java:10)
    除法结束。。。
    

    使用:(try…catch…finally)

    package exception;
    /**
     * 对产生的异常简单处理,打印出异常信息
     * 2017-09-11
     * @author Junwei Zhu
     *
     */
    public class TestException 
    {
    	public static void main(String[] args) 
    	{
    		System.out.println("除法开始。。。");
    		try 
    		{
    			System.out.println("除法开始进行:"+10/0);
    		}catch(ArithmeticException e){
    			e.printStackTrace();
    		}finally {
    			System.out.println("***这是finally语句,不管是否出现异常都会执行***");
    		}
    		System.out.println("除法结束。。。");
    	}
    }
    
    除法开始。。。
    java.lang.ArithmeticException: / by zero
    	at exception.TestException.main(TestException.java:15)
    ***这是finally语句,不管是否出现异常都会执行***
    除法结束。。。
    

    除法的两个数由args[]参数决定

      

    package exception;
    /**
     * 对产生的异常简单处理,打印出异常信息
     * 2017-09-11
     * @author Junwei Zhu
     *
     */
    public class TestException 
    {
    	public static void main(String[] args) 
    	{
    		System.out.println("除法开始。。。");
    		try 
    		{
    			int x = Integer.parseInt(args[0]);
    			int y = Integer.parseInt(args[1]);
    			System.out.println("除法开始进行:"+x/y);
    		}catch(ArithmeticException e){
    			e.printStackTrace();
    		}finally {
    			System.out.println("***这是finally语句,不管是否出现异常都会执行***");
    		}
    		System.out.println("除法结束。。。");
    	}
    }
    
    除法开始。。。
    ***这是finally语句,不管是否出现异常都会执行***
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    	at exception.TestException.main(TestException.java:15)
    【数组越界】
    

    请解释Error和Exception的区别?

    Error:是JVM错误,即:此时的程序还没有执行,如果没有执行用户无法处理;

    Exception:指的是程序运行中产生的异常,用户可以处理。

    3、异常的处理流程

    用Exception处理所有的异常。

    package exception;
    /**
     * 对产生的异常简单处理,打印出异常信息
     * 2017-09-11
     * @author Junwei Zhu
     *
     */
    public class TestException 
    {
    	public static void main(String[] args) 
    	{
    		System.out.println("除法开始。。。");
    		try 
    		{
    			System.out.println("除法开始进行:"+10/0);
    		}catch(Exception e){
    			e.printStackTrace();
    		}finally {
    			System.out.println("***这是finally语句,不管是否出现异常都会执行***");
    		}
    		System.out.println("除法结束。。。");
    	}
    }

    在开发中异常分类处理。

    4、throw、throws关键字的使用

    throws关键字

    主要用于方法声明上,指的是当方法之中出现异常后交由被调用出处理。

    package throwsTest;
    class MyMath
    {
    	public static int div(int x,int y)throws Exception{
    		return x/y;
    	}
    }
    public class TestThrows 
    {
    	public static void main(String[] args) 
    	{
    		//调用了throws声明的方法之后,那么不管操作是否出现异常,必须使用try...catch
    		//进行异常的处理
    		try {
    			System.out.println(MyMath.div(10, 2));
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    }
    

    throw关键字

    在程序之中可以直接使用throw手工的抛出一个异常类的实例化对象。

    package throwTest;
    
    public class TestThrow 
    {
    	public static void main(String[] args) 
    	{
    		try {
    			throw new Exception("自己定义的异常");
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    }
    
    java.lang.Exception: 自己定义的异常
    	at throwTest.TestThrow.main(TestThrow.java:8)
    

    请解释throw和throws的区别?

    throw:指的是在方法之中人为抛出一个异常类对象(这个异常类的对象)

    throws:在方法的声明上使用,表示此方法在调用时必须处理异常。

    5、异常处理的使用标准

    重要的代码模型:异常的使用格式

             定义的一个div()方法,要求:这个方法在进行计算之前打印提示信息,在计算结束完毕也打印提示信息,如果在计算之中产生了异常,则交给被调用处进行处理。

    package exception;
    class MyMath
    {
    	public static int div(int a,int b) throws Exception
    	{
    		int result = 0;
    		System.out.println("计算开始。。。");
    		try{
    			result = a/b;
    		}catch(Exception e){
    			e.printStackTrace();
    		}finally {
    			System.out.println("计算结束。。。");
    		}
    		return result ;
    	}
    }
    public class TestDemo 
    {
    	public static void main(String[] args) 
    	{
    		try {
    			System.out.println(MyMath.div(10, 0));
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    }
    
    计算开始。。。
    java.lang.ArithmeticException: / by zero
    	at exception.MyMath.div(TestDemo.java:10)
    	at exception.TestDemo.main(TestDemo.java:24)
    计算结束。。。
    0
    

      

    请解释Exception和RuntimeException的区别?请列举出几个你常见的RuntimeException;

    使用定义的异常必须要被处理,而RuntimeException异常可以选择性处理。

    常见的RuntimeException:ArithmeticException、NullPointerException、ClassCaseException

    --------------- 我每一次回头,都感觉自己不够努力,所以我不再回头。 ---------------
  • 相关阅读:
    AX ERROR: Could not find my mock parent, most likely I am stale 不及格的程序员
    利用Segue在视图控制器间传值的问题 不及格的程序员
    Creating a Singleton Instance 不及格的程序员
    iPad 通知 UIKeyboardWillShowNotification 不会在keyBoard处在Undock状态下接到通知 不及格的程序员
    Why RootViewController's view is rotated Automatically by System when the app first loaded? 不及格的程序员
    如何弹出UIDatePicker最好 不及格的程序员
    jQuery开始做恶了 不及格的程序员
    what is the SEL,id and IMP,Class ,Method? 不及格的程序员
    Objectivec 字符串比较的陷井 不及格的程序员
    Unable to create any keyboard shortcuts after the iOS 6.1.3 update on iPad. 不及格的程序员
  • 原文地址:https://www.cnblogs.com/zjw-blog/p/13634391.html
Copyright © 2011-2022 走看看