异常
一、初识异常:
1)异常:就是程序在运行时出现的不正常情况。
2)异常由来:java 对不正常情况进行描述后的对象体现。
对于问题的划分两种:
一种是:严重的问题,对于严重的问题,java 通过 error 类进行描述。
一种是:非严重的问题。对于非严重的问题,java 通过 Exception 类进行描述。
3)对于Exception 可以使用针对性的处理方式进行处理。
String getMessage();获取异常信息。
Throwable
|--Error
|--Exception
package study.part1.day008.exception;
public class ExceptionDemo01 {
public static void main(String[] args) {
Demo11111 d = new Demo11111();
try{
int i = d.ret(3, 0);
System.out.println("==>"+i);
//byte[] arr = new byte[1024*1024*100*100];
}catch(Exception e){
System.out.println(e.getMessage());
System.out.println(e.toString());
e.printStackTrace();
}
System.out.println("xx");
}
}
class Demo11111{
int ret(int a,int b) throws Exception,ArrayIndexOutOfBoundsException{
return a/b;
}
}
二、异常在子父类覆盖中的体现
1)子类在覆盖父类时,如果父类的方法抛出异常,那么子类的覆盖方法,只能抛出父类的异常或者该异常的子类。
2)如果父类方法抛出多个异常,那么子类在覆盖方法时,只能抛出父类异常的子集。
3)如果父类或者接口的方法中没有异常抛出,那么子类在覆盖方法时,也不可以抛出异常。如果子类方法发生了异常,就必须要进行 try 处理。绝对不能抛出。
案例 Demo
/**
* 有一个圆形和长方形,都可以获取面积
* 对于面积如果出现非法的数值,视为获取面积出现错误。
* 问题通过异常来显示,
* 现对这个程序进行基本设计。
*/
package study.base.day008.Exception;
public class ExceptionDemo2 {
public static void main(String[] args) {
Square s = new Square();
s.getArea();
OblongShape os = new OblongShape();
os.getArea();
System.exit(0); //
}
}
//他们都有一个共同的方法,计算面积
//都属于几何图形
//定义一个正方形:square
class Square implements JHTX{
//正方形要判断x=y,同时要避免空指针异常
private int x,y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
@Override
public void getArea() {
if(x<=0 || y<=0){
throw new ExceptionSel("输入的长度宽度不能为0或者负数。",x,y);
}
if(x!=y){
throw new ExceptionSel("正方形的长度宽度必须相等。",x,y);
}
System.out.println("正方形的面积是:"+(x*y));
}
}
//定义一个长方形:
class OblongShape implements JHTX{
private int x;
private int y;
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
@Override
public void getArea() {
if(x<=0 || y<=0){
throw new ExceptionSel("输入的长度宽度不能为0或者负数。",x,y);
}
System.out.println("长方形的面积是:"+(x*y));
}
}
interface JHTX {
public void getArea();
}
//自定义一个异常类,这个异常在运行时候才出错。是一个Runtime异常,
class ExceptionSel extends RuntimeException{
private static final long serialVersionUID = 1L;
private int x; //x表示宽度
private int y; //y表示长度
//重写异常类
ExceptionSel(String retuMsg,int x,int y){
super(retuMsg);
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}<span style="font-family: Arial, Helvetica, sans-serif;"> </span>
三、异常的自定义
1)描述:因为项目中会出现一些特有的问题,而这些问题并未被 java 所描述并封装对象。所以对于这些特有的问题可以按照 java 的对问题的封装思想。将这些特有的问题,进行自定义异常封装。
2)需求:在程序中,对除数是负数也视为是错误的,那么该程序是无法继续进行运算的。
package study.base.day008.Exception;
public class ExceptionSelfCreate {
public static void main(String[] args) throws Exception {
Demo001 d = new Demo001();
try{
int i = d.div(4,-1);
System.out.println("i::"+i);
}catch(FusuException ef){
System.out.println("==/"+ef.toString());
System.out.println("函数出现负数:"+ef.getValue());
}
}
}
class Demo001{
int div(int a,int b) throws FusuException{ //函数中出现了这边必须声明
if(b<0){
throw new FusuException("出现负数",b);
}
return a/b;
}
}
3)自定义的异常类:自定义异常必须是自定义类继承 Exception。
4)throws 和 throw 的区别。
throws 使用在函数上。
throw使用在函数内。
throws后面跟这是异常类,可以有多个异常类,用逗号隔开。
throw 后面跟着是异常对象。
5)当函数内容有 throw 抛出异常对象,并未进行 try 处理。必须在函数上声明,都在编译失败。
如果函数上声明了,那么函数内就不需要抛出了。
6)Exception 中有一个特殊的子类异常:RuntimeException
1、如果在函数内抛出该异常,函数上可以不用声明,编译一样通过。
2、如果在函数上声明了该异常,调用者不用处理,编译一样通过。
3、之所以不用在函数中声明,是因为不需要让调用者处理。
当该异常发生,希望终止程序。因为在运行时,出现了无法继续运算的情况,希望停止程序后,对代码进行修正。
7)对于异常的重分类理解:
1、编译时:被检测的异常。
2、编译时:不被检测的异常。(运行时异常,RuntimeException 以及其子类。)
8)异常体系的特点:
1、异常体系中的所有类以及建立的对象都具备可抛性。也就是说可以被 throws 和 throw 关键字所操作。只有异常体系具备这个特点。
2、finally:当执行了System.exit(0);后,系统 JVM 退出。finally不会被执行。
class FusuException extends RuntimeException{
private int value;
FusuException(String msg,int value){
super(msg);
this.value = value;
}
public int getValue(){
return value;
}
}