------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------
异常:是在运行时期发生的不正常情况。在java中用类的形式对不正常情况进行了描述和封装对象。描述不正常的情况的类,就称为异常类。 以前正常流程代码和问题处理代码相结合,现在将正常流程代码和问题处理代码分离,提高阅读性,其 实异常就是java通过面向对象的思想将问题封装成了 ,对象用异常类对其进行描述。
不同的问题用不同的类进行具体的描述。 比如角标越界,空指针等等。
问题很多,意味着描述的类也很多,将其共性进行向上抽取,形成了异常体系。
//数组角标越界异常
class Demo
{
public int method(int[] arr,int index)
{
if(arr==null)
throw new NullPointerException("数组的引用不能为空!");
if(index>=arr.length)
{
throw new ArrayIndexOutOfBoundsException("数组的角标越界啦,哥们,你是不是疯了?:"+index);
}
if(index<0)
{
throw new ArrayIndexOutOfBoundsException("数组的角标不能为负数,哥们,你是真疯了!:"+index);
}
return arr[index];
}
}
class ExceptionDemo2
{
public static void main(String[] args)
{
int[] arr = new int[3];
Demo d = new Demo();
int num = d.method(null,-30);
System.out.println("num="+num);
System.out.println("over");
}
}
一、异常的分类
1、
编译时异常,除了RuntimeException及其子类,Exception中所有的子类都是,这种异常必须要处理,要不编译通不过
2、
运行时异常,RuntimeException及其子类都是,这种异常不用处理,编译会通过,不过这样的程序会有安全隐患,遇到这种异常是需要改代码的
3、
严重错误问题,用Error进行描述,这个问题发生后,一般不编写针对代码进行处理,而是要对程序进行修正.通常都是由虚拟机抛出的问题
二、异常的处理方式
1、throws 抛出异常
2、try{}catch(){}捕获异常
try
{
//需要被检测异常的代码。
}
catch(异常类 变量)//该变量用于接收发生的异常对象
{
//处理异常的代码。
}
finally
{
//一定会被执行的代码。
}
要么try要么抛
class FuShuIndexException extends Exception
{
FuShuIndexException()
{}
FuShuIndexException(String msg)
{
super(msg);
}
}
class Demo
{
public int method(int[] arr,int index)//throws NullPointerException,FuShuIndexException
{
if(arr==null)
throw new NullPointerException("没有任何数组实体");
if(index<0)
throw new FuShuIndexException();
return arr[index];
}
}
class ExceptionDemo4
{
public static void main(String[] args)
{
int[] arr = new int[3];
Demo d = new Demo();
try
{
int num = d.method(null,-1);
System.out.println("num="+num);
}
catch(NullPointerException e)
{
System.out.println(e.toString());
}
catch (FuShuIndexException e)
{
System.out.println("message:"+e.getMessage());
System.out.println("string:"+e.toString());
e.printStackTrace();//jvm默认的异常处理机制就是调用异常对象的这个方法。
System.out.println("负数角标异常!!!!");
}
/*
catch(Exception e)//多catch父类的catch放在最下面。
{
}
*/
System.out.println("over");
}
}
三、Throwable(异常体系的父类)
Error(严重错误问题异常),Exception(编译时异常),自定义异常(必须继承Throwable或者其子类)
他们的共有方法有:1、String getMessage()获取异常信息,返回字符串,
2、String toString()获取异常类名和异常信息,返回字符串,覆盖了object里的toString()方法,
3、void printStackTrace()获取异常类名和异常信息,以及异常出现在程序中的位置。返回值void,void printStackTrace(PrintStream s)通常用该方法将异常内容保存在日志文件中,以便查阅。
四、throws和throw
throws用于标识函数暴露出的异常,throw用于抛出异常对象。
throws与throw的区别:thorws用在函数上,后面跟异常类名;throw用在函数内,后面跟异常对象。
class FuShuIndexException extends Exception
{
FuShuIndexException()
{}
FuShuIndexException(String msg)
{
super(msg);
}
}
class Demo
{
public int method(int[] arr,int index)//throws NullPointerException//FuShuIndexException
{
if(arr==null)
throw new NullPointerException("数组的引用不能为空!");
if(index>=arr.length)
{
throw new ArrayIndexOutOfBoundsException("数组的角标越界啦,哥们,你是不是疯了?:"+index);
}
if(index<0)
{
throw new FuShuIndexException("角标变成负数啦!!");
}
return arr[index];
}
}
class ExceptionDemo3
{
public static void main(String[] args) //throws FuShuIndexException
{
int[] arr = new int[3];
Demo d = new Demo();
int num = d.method(null,-30);
System.out.println("num="+num);
System.out.println("over");
}
}
五、异常细节
1、RuntimeException以及其子类如果在函数中被throw抛出,可以不用在函数上声明。
2、子类覆盖父类方法时,子类的方法必须抛出相同的异常或父类异常的子类。(父亲坏了,儿子不能比父亲更坏)
3、如果父类抛出了多个异常,子类覆盖父类时,只能抛出相同的异常或者是他的子集,子类不能抛出父类没有的异常
4、如果被覆盖的方法没有异常抛出,那么子类的方法绝对不可以抛出异常,如果子类方法内有异常发生,那么子类只能try,不能throws
5、当try对应多个catch时,最顶层的异常要放在最下面,反过来分析如果最顶层的异常放在第一行,那根据多态原理,后面的catch就没用了,一般面试的时候会这么问,开发的时候不会抛有继承关系的异常
子类覆盖父类只能抛出父类的异常或者子类或者子集。
注意:如果父类的方法没有抛出异常,那么子类覆盖时绝对不能抛,就只能try .
interface Inter
{
void function();
}
class D implements Inter
{
public void function()//throws Exception
{}
}
class A extends Exception
{
}
class B extends A
{
}
class C extends Exception
{
}
Exception
|--A
|--B
|--C
class Fu
{
void show()throws A
{}
}
class Test
{
void method(Fu f)//Fu f = new Zi();
{
try
{
f.show();
}
catch (A a)
{
}
}
}
class Zi extends Fu
{
void show()throws C
{
}
}
class
{
public static void main(String[] args)
{
Test t = new Test();
t.show(new Zi());
}
}
interface Inter
{
void function();
}
class D implements Inter
{
public void function()//throws Exception
{}
}
class A extends Exception
{
}
class B extends A
{
}
class C extends Exception
{
}
Exception
|--A
|--B
|--C
class Fu
{
void show()throws A
{}
}
class Test
{
void method(Fu f)//Fu f = new Zi();
{
try
{
f.show();
}
catch (A a)
{
}
}
}
class Zi extends Fu
{
void show()throws C
{
}
}
class
{
public static void main(String[] args)
{
Test t = new Test();
t.show(new Zi());
}
}