/*
异常处理机制:
1.异常:就是程序在运行时出现不正常的情况。
异常的由来:问题是生活中一个具体的事物,通过java的类的形式进行描叙,
并封装成对象,其实就是java对不正常情况进行描叙后的对象体现。
Throwable
|--Error
|--Exception
严重的,java通过Error类进行描叙
非严重的,java通过Exception类进行描叙
2.异常的处理
java提供的特有的语句处理
try
{
需要被检测的代码
}
catch()
{
处理异常的代码;(处理方式)
}
finally
{
一定会执行的语句
}
*/
class Demo
{
int div(int a,int b)throws Exception//在功能上通过throws的关键字声明该功能可能会出现异常
//调用该函数的必须声明异常或者处理异常
{
return a/b; //new ArithmeticException()
}
}
class ExceptionDemo1
{
public static void main(String[] args)
{
Demo d = new Demo();
try
{
int x = d.div(4,0);//new ArithmeticException()
System.out.println("x="+x);
}
catch (Exception e)// Exception e = new ArithmeticException();多态的表现
{
System.out.println(e.getMessage());//by zero
System.out.println(e.toString());//异常名称:异常信息
e.printStackTrace();//异常名称:异常信息 异常出现的位置
//jvm默认的异常处理机制,就是在调用printStackTrace方法,
//打印异常堆栈的跟踪信息
}
finally
{
System.out.println("over");
}
}
}
---------------------
/*
对多异常的处理
1.声明异常时,建议声明更为具体的异常,这样处理可以更具体
2.对方声明几个异常,就对应几个catch块,不要定义多余的catch块,因为如果出现其他异常的情况,我们希望的就是程序能够结束
如果多个catch块中的异常出现继承关系,父类异常catch块放在最下面
在进行catch处理的时候,不要简单的定义一句输出语句,一定要定义具体处理方式
(1).把出现的异常信息都记录到异常日志里
*/
class Demo
{
int div(int a,int b)throws ArithmeticException,ArrayIndexOutOfBoundsException//在功能上通过throws的关键字声明该功能可能会出现异
{
int[] arr = new int[a];
System.out.println(arr[4]);//两个异常不能同时发生,角标一越界,这个函数就结束了
return a/b;
}
}
class ExceptionDemo2
{
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());
}
*/
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println(e.toString());
System.out.println("角标越界");
}
catch(ArithmeticException e)
{
System.out.println(e.toString());
System.out.println("被零除了");
}
/*
catch(Exception e)//没声明的异常不要写catch捕捉,否则发生其他异常后,程序不会结束,我们就不知道哪里发生问题了
{
System.out.println(e.toString());
}
*/
finally
{
System.out.println("over");
}
}
}
-------------------
/*
自定义异常:
因为一些项目中会出现特有问题,而这些问题未被java所描叙
所以对于这些特有问题,可以按照java的对问题封装的思想
将特有的问题,进行自定义的异常封装
需求:在该程序中,对于除数为-1的情况,也视为无法进行运算的
那么就需要对该问题进行自定义的描叙
当程序编译的时候出现异常错误,证明语法已经没有错误了
编译时期,是先检测基本的语法错误,然后再检测异常错误的
当在函数内部出现了throw抛出异常对象,那么就必须要给对应的处理动作。
要么在函数声明异常,让调用者处理
要么在内部try catch 处理
一般情况下,在函数内出现异常,函数上需要声明
*/
class FuShuException extends Exception
{
}
class Demo
{
int div(int a,int b)throws FuShuException//不声明会出现:未报告的异常错误FuShuException
{
if(b<0)
throw new FuShuException();//手动通过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());//打印结果只有异常的名称,没有异常的信息,因为没有定义异常信息
System.out.println("除数出现负数了");
}
finally
{
System.out.println("over");
}
}
}
/*
如何定义异常信息
因为父类中已经把异常信息的操作都完成了
所以子类只要在构造时,将异常信息传递给父类通过super语句
那么就可以直接通过getMessage方法获取自定义的异常信息
*/
//方式一
class FuShuException extends Exception
{
private String msg;
FuShuException(String msg)
{
this.msg = msg;
}
public String getMessage()
{
return msg;
}
}
//方法二原理
class Throwable
{
private String message;
Throwable(String message)
{
this.message = message;
}
public String getMessage()
{
return message;
}
}
class Exception extends Throwable
{
}
//方法二
class FuShuException extends Exception
{
private int value;
FuShuException()
{
super();
}
FuShuException(String msg,int value)
{
super(msg);
this.value = value;
}
public int getValue()
{
return value;
}
}
class Demo
{
int div(int a,int b)throws FuShuException//不声明会出现:未报告的异常错误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());
// System.out.println("除数出现负数了");
System.out.println("错误的负数是"+e.getValue());
}
finally
{
System.out.println("over");
}
}
}
/*
自定义异常:
必须是自定义类继承Exception
继承Exception的原因:
异常体系有一个特点,因为异常类和异常对象都被抛出
他们都具备可抛性,这个可抛性Throwable这个体系中特有的特点
只有这个体系中的类和对象才可以被throw和throws操作
throw和throws的区别
throws使用在函数上
throw使用在函数内
throws是后面跟的是异常类,可以跟多个,用逗号隔开
throw后跟的是异常对象
-------------------------
Exception中有一个特殊的子类异常 RuntimeException运行时异常
如果在函数内容抛出该异常,函数可以不用声明,编译一样通过
如果在函数上声明了该异常,调用者可以不用上进行处理,编译一样通过
之所以不用在函数声明,是因为不需要让调用者出来。
当该异常发生,希望程序停止,因为运行时,出现了无法继续运算的情况,
希望停止程序,对代码进行修正。
自定义异常时,如果该异常的发生,无法再继续进行运算
就让该自定义异常继承RuntimeException
对于异常分两种:
1.编译时被检测的异常
--必须try或者抛,因为这个异常可以处理
2.编译时不检测的异常(运行时异常,RuntimeException以及其子类)
-- 会先查看是否是RuntimeException以及其子类异常,是的话就不管
*/
class FuShuException extends RuntimeException
{
FuShuException(String msg)
{
super(msg);
}
}
class Demo
{
int div(int a,int b)//throws FuShuException,因为下面两个异常都是RuntimeException异常,所以不用声明异常
{
if(b<0)
throw new FuShuException("除数出现负数了");
if(b==0)
throw new ArithmeticException();
return a/b;
}
}
class ExceptionDemo4
{
public static void main(String[] args)
{
Demo d = new Demo();
int x = d.div(4,-1);
System.out.println("x="+x);
System.out.println("over");
}
}
黑马程序员_异常
最新推荐文章于 2015-04-13 00:30:52 发布