---------------------- ASP.Net+Unity开发、.Net培训、期待与您交流! ----------------------
异常概述:
异常就是程序在运行的时候出现的不正常情况
异常的由来:
问题也是生活中一个具体的事物,
也可以通过java类的形式进行描述并封装成对象,
其实就是java对不正常情况进行描述后的对象体现
对于问题的划分:
两种:一种是严重的问题,一种是非严重问题 - -|||
严重:
java通过Error类进行描述
对于Error一般不编写针对性的代码对其进行处理
非严重:
java通过Exception类进行描述
对于Exception可以使用针对性的处理方式进行处理
框架:
Throwable
|--Error
|--Exception
可处理异常分两种:
编译时被检测的异常(可以处理)
编译时不被检测的异常(运行是异常,RuntimeException及其子类)(不可以处理)
Exception
捕获异常:try-catch
Java 提供了特有的语句进行处理
try
{
需要被检测的代码
}
catch(异常类变量)
{
处理异常的代码(处理方式)(像医院,问题出来了怎么解决就要看个人的能力了)
}
finally
{
一定会执行的语句
}
示例:
class Demo
{
int div(int a ,int b)
{
return a/b;
}
}
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)
{
System.out.println(“除数不能为零”);
System.out.println(e.getMessage());// / by zero;
System.out.println(e.toString());// 异常类名异常信息
e.printStackTrace();// 异常名称异常信息异常位置
//其实jvm默认的异常处理机制,就是在调用printStackTrace方法,打印异常
//的堆栈的跟踪信息
}
}
}
异常声明throws
throws Exception:在功能上通过throws的关键字声明了该功能有可能会出现问题
class Demo
{
int div(int a ,int b) throws Exception //将问题往上抛,让调用者处理,就像传球一样
{
return a/b;
}
}
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)
{
System.out.println(e.toString());// 异常类名异常信息
}
System.out.println("over");
}
}
多异常处理:
声明异常的时候尽量具体一些,这样便于发现问题,而越详细就会抛出的越多
处理:
对方声明几个异常就对应几个catch块
如果多个catch块中的异常出现继承关系,父类异常catch块放在最下面,也就是说抛得越大的异常越晚处理
class Demo
{
int div(int a ,int b) throws ArithmeticException, ArrayIndexOutOfBoundsException
{
int[] arr = new int[a];
System.out.println(arr[4]);
return a/b;
}
}
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(ArithmeticException e)
{
System.out.println(e.toString());// 异常类名异常信息
System.out.println("被零除了");
}
catch(ArrayIndexOutOfBoundsException e2)
{
System.out.println(e.toString());
System.out.println("数据角标越界了");
}
System.out.println(“over”);
}
}
自定义异常:
项目中有时会出现特有的问题,而这些问题并未被java所描述并封装对象
所以对于这些特有的问题可以按照java的对问题封装的思想
将特有的问题,进行自定义异常封装
定义异常信息:
class NegativeNumberException extends Exception
{
private String msg;
NegativeNumberException(String msg)
{
this.msg = msg;
}
public String getMesssage()
{
return msg;
}
}
class Test
{
int div(int a ,int b) throws NegativeNumberException
{
if(b<0)
throw new NegativeNumberException("除数为负数了");
return a/b;
}
}
class Demo
{
public static void main(String[] args)
{
try
{
Test t = new Test();
int x = t.div(4,0);
System.out.println("x="+x);
}
catch(NegativeNumberException e)
{
System.out.println(e.toString());
}
}
}
因为父类中已经把异常信息的操作都完成了
所以子类只要在构造时,将异常信息传递给父类通过super语句
那么就可以直接通过getMessage方法获取自定义的异常信息
使用super语句:
class NegativeNumberException extends Exception
{
NegativeNumberException(String msg)
{
super(msg);
}
}
传递多值的异常
class NegativeNumberException extends Exception
{
private int value;
NegativeNumberException()
{
super();
}
//传递了消息msg,还有值value
NegativeNumberException(String msg,int value)
{
//通过父类有参构造函数传递消息
super(msg);
this.value = value;
}
//自定方法获取值
public int getValue()
{
return value;
}
}
继承Exception的原因:
异常体系有一个特点:因为异常类和异常对象都被抛出
他们都具备可抛性,这个可抛性是Throwable这个体系中独有特点
只有这个体系中的类和对象才可以被throws和throw操作
throws和throw的区别:
throws使用在函数上
throws后面跟的是异常类,可以跟多个,用逗号隔开
throw使用在函数内
throw后面跟的是异常对象
RuntimeException
Exception中的特殊异常,运行时异常
特点:
如果在函数内容抛出该异常,函数上可以不用声明,编译一样通过
如果在函数上声明该异常,调用着可以不用处理,编译一样通过
当该异常发生时,希望程序停止,因为在运行时,出现了无法继续运算的情况,希望停止程序后,对代码进行修正
自定义异常时:
如果想让异常只在发生时,无法在继续进行计算
就让自定义异常继承RuntimeException
class NegativeNumberException extends RuntimeException //1、继承RuntimeException
{
NegativeNumberException(String msg)
{
super(msg);
}
}
class Test
{
int div(int a ,int b) //2、不用声明了
{
if(b<0)
throw new NegativeNumberException(“负数啦”);
return a/b;
}
}
class Demo
{
public static void main(String[] args)
{
try
{
Test t = new Test();
int x = t.div(4,0);
System.out.println("x="+x);
}
catch(NegativeNumberException e)
{
System.out.println(e.toString());
}
}
}
感觉实现了RuntimeException就说明一个道理:不作不死
---------------------- ASP.Net+Unity开发、.Net培训、期待与您交流! ----------------------详细请查看:www.itheima.com