Java基础-异常

来源:互联网 发布:知敬畏守底线心得 编辑:程序博客网 时间:2024/05/17 21:54


一.异常概述

异常:就是程序在运行时出现不正常情况。

(1)异常由来:问题也是现实生活中一个具体的事物,也可以通过java的类的形式进行描述,并封装成对象。

其实就是java对不正常情况进行描述后的对象体现。


(2)对于异常问题的划分    //2

1.一种是严重的问题

对于严重的,java通过error类进行描述

对于error一般不编写针对性的代码对其进行处理。

对于非严重的,java通过execption类进行描述。

2.一种非严重的问题。

对于execption可以使用针对性的处理方式进行处理。

无论error或者execption都具有一些共性内容。

比如:不正常情况的信息,引发原因等。


异常有两种:

        编译是被检测到的异常     //4

                该异常在编译时,如果没有处理(没有抛也没有try),编译失败。
                该异常被标识,代表这可以被处理。

        运行时异常(编译时不检测)

                在编译时,不需要处理,编译器不检查。
                该异常的发生,建议不处理,让程序停止,需要对代码进行修正

Throwable   //3

(1)--Error:指的是底层的、低级的、不可恢复的严重错误。此时程序一定会退出,

因为已经失去了运行所必须的物理环境,一般不编译特定的代码进行处理


(2)--Exception:要用针对性的处理方式进行处理

--RunntimeException:运行时异常,可以不进行处理
这类异常可以不捕获或声明,编译可以通过,因为需要出错时爆出来然后解决掉他!!!!


--非RunntimeException:要进行处理,这类异常要捕获或是声明,不然编译无法通过

二.异常的处理方式

第一种方式:捕获

  (1).后面可以跟多个catch异常对象,但异常对象应该按由子类到父类的方式排列
   如果父类异常排在子类的前面,那么后面的子类异常会执行不到
   
try
{
可能出现异常的代码
}
catch(异常类型,变量)
{
异常处理方式
}
  
  (2).finally是一定会执行的语句,一般用来关闭资源
  
try
{
 可能出现异常的代码
}
catch(异常类型,变量)
{
异常处理方式
}
finally
{
 一定会执行的语句
}
  
  (3).其实这种方式并不能捕获异常,因为没有catch进行处理,当try中出现异常时,程序还是
   会直接抛出异常给上级(指的运行时异常,非运行时异常编译不通过)
  
  try
  {
   可能会出现异常的代码
  }
  finally
  {
   一定会执行的语句
  }

第二种方式:声明  //2

在函数内部直接将异常抛出,并在函数上声明这个异常,将异常交由上一级处理

  throw与throws区别

  throw:用在函数内部,在函数内部抛出异常,后面跟的是异常对象
  throws:用在函数上,后面跟的是异常类,并且可以跟多个异常类,中间有逗号分开


第一种方式:捕获异常(代码实现)

public class ExceptionTest{public static void main(String[] args){div(4,7);System.out.println("main code");}public static void div(int a,int b){  //捕获异常,当出现异常时,语句直接跳转到catch中进行异常的处理,try中出现异常的后面的语句就不会执行了try{int temp=a/b;int[] num=new int[a];System.out.println("temp="+temp);System.out.println(num[a]); }   catch(ArithmeticException e){ e.printStackTrace(); //try后可以跟多个catch,应按照子类到父类的排列顺序,//如果try中出现了catch中所有的异常,那么程序会直接抛出异常给上一级   }   catch(ArrayIndexOutOfBoundsException e){e.printStackTrace();//finally中执行的一般是关闭资源的操作   }   finally{ System.out.println("一定会执行的语句"); }}}

第二种方式:声明异常(代码实现)

public class ExceptionTest{public static void main(String[] args) throws Exception{//调用者可以进行try或在函数上声明,如果进行了try那后面的语句还会执行,只是声明那后面的语句就不会执行了div(4,0); System.out.println("main code");}//在函数上声明异常,将异常交由上一级处理public static void div(int a,int b) throws Exception{int temp=a/b;System.out.println("temp="+temp);}}

第三.打印异常信息的原理

class Demo{int div(int a,int b){return a/b;  //会产生 new aritchmeticexception(),以前是会传给虚拟机,但是现在有了catch就传给catch了}}class ExceptionDemo{public static void main(String [] args){Demo d = new Demo();try{int x = d.div(4,0);  //在这里会产生System.out.println("x="+x);} catch (Exception e)  // exception e = new arithmeticexception();{//System.out.println("除零了");System.out.println(e.getMessage());// by zero//获取异常信息/ by zero//System.out.println(e.toString());//异常名称:异常信息。java.lang.ArithmeticException: / by zero//e.printStackTrace();//异常名称,异常信息,异常出现的位置。//其实jvm的默认的异常处理机制,就是在调用printstacktrace//打印异常的堆栈的跟踪信息//java.lang.ArithmeticException: / by zero//at cn.itcast.exception.Demo.div(ExceptionDemo.java:7)//at cn.itcast.exception.ExceptionDemo.main(ExceptionDemo.java:18)}}}

三.多异常的处理

1.声明异常时,建议声明更为具体的异常。这样处理的可以更具体
2.对方声明几个异常,就对应有几个catch块,不要定义多余的catch块,如果多个catch 块中异常出现继承关系,父类异常catch块放在最下面(!!!!),建议在进行catch处理时,catch中一定要定义具体处理方式。
也不要简单的就书写一条输出语句。

class Demo{int div(int a , int b) throws airthmeticexception,arrayindexoutofboundsexcwption //一个是算数异常,一个是越界{Int[] arr = new int[a];System.out.println(arr[4]); return a/b;}}class Exceptiondemo1                                     {public static void main(String[] args)throws exception    {Demo d = new Demo();try{int x = d.div(4,1);System.out.println("x="+x);}catch (airthmeticexception e){System.out.println(e.tostring());System.out.println("被零除了!!");}catch (arrayindexoutofboundsexcwption e){System.out.println(e.tostring());System.out.println("角标越界啦!!");}catch (excwption e)   //父类异常catch块处理,父类异常放最后!!!{System.out.println(e.toString());}System.out.printlln("over");}}

四.自定义异常

自定义异常类:必须是自定义类继承Exception类体系
继承Exception原因:
异常体系有一个特点,因为异常类和异常对象都要被抛出
他们都具备可抛性,这个可抛性是throwable这个体系中独有特点。
只有这个体系中的类和对象才可以被throws和throw操作。



如果不想对自定义的异常类进行处理,可以让自定义类继承RuntimeException
因为项目中会出现特有的问题
而这些问题并未被java所描述并封装对象。
所以对于这些特有的问题都可以按照java的对问题封装的思想
将特有的问题,进行自定义一个异常封装


自定义异常。

需求:在本程序中,对于除数是-1,也视为是错误的是无法进行运算的,那么就需要对这个问题进行自定义的描述。

当在函数内部出现了throws抛出异常对象,那么就必须要给对应的处理动作。
1.要么在内部try catch处理。
2.要么在函数上声明让调用者处理。


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

throws和throw的区别
throws使用在函数上。
throw使用在函数内。
throws后面跟的异常类,可以跟多个,用逗号隔开。
throw后跟的是异常对象。

class FuShuException extends Exception {/*  用super(msg)就可以了private String msg;FuShuException(String msg){this.msg = msg;}public String getMessage()//发现打印的结果中只有异常的名称,却没有异常的信息,因为自定义的异常并未定义信息(getmessage){return msg;}*/private int value;FuShuException(){super();}FuShuException(String msg, int value){super(msg);//如何定义异常信息呢?//因为父类中已经把异常信息的操作都完成了。//所以子类只要在构造时,将异常信息传递给父类通过super语句。//那么就可以直接通过getMessage方法获取自定义的异常信息this.value = value;}public int getValue(){return value;}}class Demo{//这里抛出FuShuException是因为一般情况下,函数内出现异常,函数上也需要声明!!!int div(int a , int b)throws FuShuException{if(b<0)throw new FuShuException("出现了除数是负数的情况",b);//手动通过throw关键字抛出一个自定义异常对象return a/b;  //除零自动抛出,负数只能手动抛出}}class ExceptionDemo3{public static void main(String[] args){Demo d= new Demo();try{int x = d.div(4,-1); System.out.println("x="+x);}catch (FuShuException e){System.out.println(e.toString()); //toString会自动调用getMessage//System.out.println("除数出现负数了");System.out.println("错误" + e.getValue());}System.out.println("over");}}

五。异常-处理语句其他格式

第一个格式

try
{

}
catch ()
{
}


第二个格式
try
{

}
catch ()
{
}
finally 
{
}
第三个格式:
try
{

}
finally ()
{
}


//记住一点,catch用于处理异常,如果没有catch就代表异常没有被处理过,如果该异常是检测时异常,那么必须声明

class Demo{public void method(){/*try{throw new Exception(); //问题在这里用catch解决了,就不用去method上面声明,如果没有try catch就要申明,否则会报错}catch (Exception e){throw e;}*/try{throw new Exception(); //这里method就要声明,否则编译报错,因为没有解决}finally {//关资源}}}class  {public static void main(String[] args) {System.out.println("Hello World!");}}

六.异常覆盖时的特点

异常在字符类覆盖中的体现:
1.子类再覆盖父类时,如果父类的方法抛出异常,那么子类的覆盖方法,只能抛出父类的异常或者该异常的子类
2.如果父类抛出多个异常,那么子类在覆盖该方法时,只能抛出父类异常的子集(也就是其中一个)。!!!
3.如果父类或接口的方法中没有异常抛出,那么子类在覆盖方法时,也不可以抛出异常。如果子类方法发生了异常。就必须要进行try处理,绝对不能抛
注意如果接口没有异常抛出,那么子类内部就不能抛出异常!!!!!这就是为什么继承Runable  里面的wait不能抛异常的原因
如果父类或者接口的方法中没有异常抛出,那么子类在覆盖方法时,也不可以抛出异常,如果子类发生了异常,只能进行try
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


class AException extends Exception{}class BException extends AException{}class CException extends Exception{}/*Exception|--AException|--BException|--CException*/class Fu{void show() throws AException{}}class  Test{void function (Fu f){try{f.show();}catch (AException e ){}}}class Zi extends Fu{void show throws Bexception //这里就不能抛父类{}}class  {public static void main(String[] args) {//System.out.println("Hello World!");Test t = new Test();t.function(new zi());//可以看到zi中抛出CException,但是只能处理AException,处理不了CException}}


七.有关finally的问题

1,finally的执行时间问题!!!!

  finally语句是在return语句之后,跳转到上一级程序之前执行(即return语句中间执行)
  finally中的语句一定会执行,是建立在对应的try得到执行情况下,如果程序在try语句之前就发生了
  异常或就已经结束,那finally中的语句是不会执行的
  finally在一种情况下不会执行,System.exit(0):程序停止运行,退出虚拟机!!!!!!!!
public class ExceptionDemo{public static void main(String[] args){System.out.println(test());}public static String test(){       try     {     System.out .println("try block");     return test1 ();       }      finally      {     System.out .println("finally block"); //  return "finally";// 如果finally中有返回值,那么finally返回值就会替换掉上一级方法中的返回值   //这一句如果执行,那程序上一级方法中的after return 就会被finally所替换  //有     // try block  // return statement  // finally block  //finally     //没有  // try block  // return statement  // finally block  // after return     }  }  public static String test1(){    System.out.println("return statement");    return "after return";  }}/*程序结果为:  try block  return statement  finally block  after return原因分析:   1.try语句块,return test1(),则调用test1方法   2.test1()执行后返回"after return",返回值"after return"保存在一个临时区域里   3.执行finally语句块。若finally语句有返回值,则此返回值将替换掉临时区域的返回值   4.将临时区域的返回值送到上一级方法中。*/public class ExceptionTest{public static void main(String[] args){int a=5;int[] arr=new int[5];//return 0;要想让方法停止运行可以return或throw个异常int i=a/0;try{ System.out.println(arr[6]);} catch(ArrayIndexOutOfBoundsException e) {   System.out.println(e.getMessage());   System.out.println("角标越界");  //这里的finally语句是不会执行的,因为在try语句之前程序就已经停止运行了 } finally{System.out.println("finally"); }}}

2..finally代码块:定义一定执行的代码,通常用于关闭资源

/*
finally代码块:定义一定执行的代码
通常用于关闭资源
*/
class FuShuException extends Exception{FuShuException(String msg){super(msg);}}class Demo{int div(int a , int b )throws FuShuException{if(b<0)throw new FuShuException("除数为负数");return a/b;}}class ExceptionDemo6{public static void main(String[] args){Demo d =new Demo();try{int x = d.div(4,-1);System.out.println("x="+x);}catch (FuShuException e){System.out.println(e.toString());}finally{//即使catch中有return也会执行finallySystem.out.println("finally");//finally中存放的代码是一定会执行的}System.out.println("over");}}class NoException extends Exception{}//数据库类似于服务器,可以建立的连接数量有限,所以用完就要释放,要不然别人就没法用//这里不能throws SQLException ,因为调用method的对象不懂SQLException//所以抛出也没用,这里只能抛出throws Noexception(数据没存成功)//类似于销售问仓库要货,仓库发现要的货发霉了(SQLException)//发霉了只能仓库处理,但是仓库要告诉销售没有货(Noexception)public void method() throws Noexception{连接数据库;数据操作;//throw new SQLException();关闭数据库;//该动作,无论数据操作是否成功,一定要关闭资源try{连接数据库;数据操作;//throw new SQLException();}catch (SQLException e){会对数据库进行异常处理;throw new NoException();}finally{关闭数据库;//该动作,无论数据操作是否成功,一定要关闭资源}}



0 0