zoukankan      html  css  js  c++  java
  • 【java】异常

    异常体系:
    Throwable
    ---Error
    通常出现重大问题:如类不存在或者内存溢出。停止运行,不对代码进行处理
    ---Exception
    在运行时出现的情况,可以进行针对性处理。可以通过try catch finally

      ---RuntimeException  运行时异常。


    异常处理:

    try
    {
        需要被检测的代码
    }
    catch (异常类 变量)
    {
        处理异常的代码
    }
    finally
    {
        一定会执行的代码
      通常是关闭资源或连接。 }

    例子:

    class Div
    {
    	private int a;
    	private int b;
    	public int div(int a,int b)
    	{
    		this.a=a;
    		this.b=b;
    		return a/b;
    	}
    
    }
    
    
    class  DivDemo
    {
    	public static void main(String[] args) 
    	{
    		try
    		{
    			Div d1=new Div();
    			int x=d1.div(6,0);		
    			System.out.println(x);
    		}
    		catch (Exception e)
    		{
    			System.out.println(e.getMessage());/// by zero
    			System.out.println(e.toString());///java.lang.ArithmeticException: / by zero
    			e.printStackTrace();//详细的堆栈信息
    								 //java.lang.ArithmeticException: / by zero
    								 // at Div.div(Divdemo.java:9)
    								 // at DivDemo.main(Divdemo.java:22)
    		}
    		
    	}
    }
    

      

    对多异常的处理:
    1.声明异常时,建议声明更为具体的异常。这样处理的可以具体些
    2.对方声明几个异常,就对应有几个catch块,不要定义多余的catch块。
    3.如果多个catch块中的异常出现继承关系,父类异常一定要放在最下面

    自定义异常:
    当函数内部出现了throw抛出异常对象,那么就必须给对应的处理动作。
    1.要么在内部try catch处理
    2.要么在函数上声明让调用者处理
    3.如何自定义异常信息:因为父类中已经异常信息的操作都完成了。所以子类只要在构造时,讲异常信息传递给父类,通过super语句。

     一般情况,函数内部出现异常,函数上需要声明

    4.自定义类必须继承Exception.
    5.throws和throw的区别:
      1)throws 使用在函数上,用于函数声明;throw 使用在函数内。
      2)throws 后面紧跟着声明的异常类,可以多个,用逗号隔开;throw后面跟着是抛出异常类的对象。

    6.catch 是用于处理异常。如果没有catch就代表异常没有被处理过,如果该异常是检测时异常,那么必须声明

    7.finall一种情况下不会执行,当程序执行到System.exit(0),finally不会执行。例如:

    class Test
    { 
    	public static String output=""; 
    	public static void foo(int i)
    	{ 
    		try
    		{ 
    			if(i==1)
    				throw new Exception(); 	
    			output+="1"; 
    		} 
    		catch(Exception e)
    		{ 
    			output+="2"; 
    			return; //returun后finally继续执行,但是其他语句就不再执行了
    		} 
    		finally
    		{ 
    			output+="3"; 
    		} 
    		output+="4"; 
    	}
    	public static void main(String args[])
    	{ 
    		foo(0);
    		System.out.println(output);//134
    		foo(1); 
    		System.out.println(output); //13423
    	}
    } 

    8.throw单独存在,下面不要定义语句,因为执行不到。

    class FushuException extends Exception
    {
    	FushuException(String msg)
    	{
    		super(msg);
    	}
    }
    
    class Div
    {
    	private int a;
    	private int b;
    	public int div(int a,int b) throws FushuException//函数内部出现throw抛出异常,函数上需要throws声明。
    	{
    		this.a=a;
    		this.b=b;
    		if (b<0)
    		{
    			throw new FushuException("出现了负数");
                   //System.out.print("dddd");//throw单独存在,下面不要定义语句,因为执行不到。 } return a/b; } } class FuDemo { public static void main(String[] args) { try { Div D1=new Div(); D1.div(3,-1); } catch (FushuException e) { System.out.print(e.toString()); } } }

     

    Exception 中有一个特殊的子类异常叫RuntimeException 运行时异常。特点:出现了无法运行的情况,需要对代码进行修改

    1.如果在函数内throw抛出异常,函数上可以不用声明,编译一样通过。
    2.如果在函数上声明了该异常,调用者可以不用进行处理,编译一样通过。

    自定义异常时:如果该类的发生,无法在继续进行运行,可让自定义异常类继承RuntimeException

    异常分类:
    1.编译时被检测的异常
    2.编译时不被检测的异常(RuntimeException以及子类)

    catch内,需要定义针对性的处理方式。不要简单的定义printStackTrace,输出语句。
    		也不要不写。
    		当捕获到的异常,本功能处理不了时,可以继续在catch中抛出。
    		try
    		{
    			throw new AException();
    		}
    		catch (AException e)
    		{
    			throw e;
    		}
    
    		如果该异常处理不了,但并不属于该功能出现的异常。
    		可以将异常转换后,在抛出和该功能相关的异常。
    
    		或者异常可以处理,当需要将异常产生的和本功能相关的问题提供出去,
    		当调用者知道。并处理。也可以将捕获异常处理后,转换新的异常。
    		try
    		{
    			throw new AException();
    		}
    		catch (AException e)
    		{
    			// 对AException处理。
    			throw new BException();
    		}
    
    		比如,汇款的例子。
    

      

    异常在子父类覆盖中的体现:
    1.子类在覆盖父类时,如果父类的方法抛出异常,那么子类的覆盖方法,只能抛出父类的异常或者该异常的子类。
    2.如果父类方法抛出多个异常,那么子类在覆盖该方法时,只能抛出父类异常的子集。
    3.如果父类或者接口的方法没有异常抛出,那么子类在覆盖方法时,也不可以抛出异常。
    如果子类方法发生了异常,必须进行try处理,绝对不能抛

    class NoValueException extends RuntimeException
    {
    	NoValueException(String message)
    	{
    		super(message);
    	}
    }
    
    interface Shape
    {
    	void getArea();
    }
    
    class Rec implements Shape
    {
    	private int len,wid;
    
    	Rec(int len ,int wid)//throws NoValueException
    	{
    		if(len<=0 || wid<=0)
    			throw new NoValueException("出现非法值");
    
    		this.len = len;
    		this.wid = wid;
    	}
    
    	public void getArea()
    	{
    		System.out.println(len*wid);
    	}
    }
    
    
    class Circle implements Shape
    {
    	private int radius;
    	public static final double PI = 3.14;
    
    	Circle(int radius)
    	{
    		if(radius<=0)
    			throw new NoValueException("非法");
    		this.radius = radius;
    	}
    
    	public void getArea()
    	{
    		System.out.println(radius*radius*PI);
    	}
    }
    
    
    
    
    class  ExceptionTest1
    {
    	public static void main(String[] args) 
    	{	
    		
    		Rec r = new Rec(3,4);
    		r.getArea();
    
    		Circle c = new Circle(-8);
    
    		System.out.println("over");
    	
    	}
    }
    

      

  • 相关阅读:
    UNIX常用shell
    exit函数
    linux消息队列
    互斥量
    RCS版本控制
    linux samba
    UML建模
    linux syslog
    python基础-列表List及内置方法
    仿美团详情页与购物车源码-详情页
  • 原文地址:https://www.cnblogs.com/paulwinflo/p/8043202.html
Copyright © 2011-2022 走看看