黑马程序员技术交流社区
标题:
Java基础学习日记 - 异常
[打印本页]
作者:
qihuan
时间:
2015-7-5 21:54
标题:
Java基础学习日记 - 异常
package practice;
/**
* 异常:程序在运行时出现的不正常情况。
* -----------------------------------------------------------------------
* 1.异常由来:Java对不正常情况进行描述后的对象体现。
* 问题划分: 一种是严重的问题,一种是非严重的问题。
* 严重的通过Error类描述,非严重的Java通过Exception类进行描述。
* 对于Error一般不编写针对性的代码进行处理。对于非严重的,Java通过Exception类进行描述。
* 无论Error或Exception都具有一些共性的内容。
* Throwable
* |--Error
* |--Exception
* |--编译时可检测的异常,只要是Exception及其子类都是编译时被检测的异常
* |--RuntimeException,这个异常是编译时不被检查的异常。
* ------------------------------------------------------------------------
* 2.异常处理:Java提供了特有的语句进行处理。
* try {需要被检测的代码}
* catch(异常类 变量) {处理异常的代码}
* finally {一定会执行的语句}
* --> finally:通常用于关闭资源。
* --> finally只有一种情况不会执行,就是当执行到:System.exit(0);
* ------------------------------------------------------------------------
* 3.对捕获到的异常对象进行常见方法操作:
* (1)String getMessage()
* (2)toString()
* (3)printStackTrace()
* ------------------------------------------------------------------------
* 4.对于多异常的处理:
* (1)声明异常的时候,建议声明更具体的异常,这样可以处理的更具体。
* (2)对方声明几个异常,就对应有几个catch块,不要定义多余的catch块。
* 如果有多个catch块中的异常出现继承关系,父类异常catch块放在最下面。
* Tips:建议在进行catch处理时,catch中一定要定义具体处理方式。不要简单定义一句e.printStackTrace(),
* 也不要简单的书写一条输出语句。
* ------------------------------------------------------------------------
* 5.自定义异常:* 因项目中会出现特有的问题,而这些问题并没有被Java所描述并封装对象,
* 所以对于这些特有的问题可以按照Java的对问题封装的思想,将特有的问题,
* 进行自定义的异常封装。
* * 自定义异常定义异常信息:
* 因为父类已经把异常信息的操作都已经完成了,所以子类只要在构造函数时,
* 将异常信息通过super语句传给父类,那么就可以直接通过getMessage方法,
* 获取自定义的异常信息了。
* * Tips:自定义类必须继承Exception。(因为异常类和异常对象都需要被抛出,它们都具备可抛性,
* 这是Throwable独有的特点,只有这个体系中的类和对象才可以被throws和throw操作。)
* * throw和throws的区别:
* (1)throws使用在函数上,throw使用在函数内。
* (2)throws后面跟的是异常类,可以跟多个,之间用“,”隔开。throw跟的是异常对象。
* * 实例:需求是在本程序中,对于除数是负数,也视为错误。那么就要对这个问题进行自定义的描述。
* ------------------------------------------------------------------------
* 6.RuntimeException: (1)如果在函数抛出该异常,函数上可以不用声明,编译一样通过。
* (2)如果在函数上声明了该异常,调用者可以不用进行处理,编译一样通过。
* 这时因为不需要让调用者处理,当该异常发生,希望程序停止,由程序员对代码进行修正。
* 引出-->自定义异常时,如果该异常的发生,无法再继续运算,就让自定义异常继承RuntimeException。
* ------------------------------------------------------------------------
* 7.异常在子父类覆盖中的体现:
* (1)子类在覆盖父类时,如果父类的方法抛出异常,那么子类的覆盖方法只能抛出父类的异常或者该异常的子类。
* (2)如果父类方法抛出多个异常,那么子类在覆盖该方法时,只能抛出父类异常的子集。
* (3)如果父类或接口的方法中没有异常抛出,那么子类再覆盖方法是,也不可以抛出异常处理。
* 如果子类方法发生了异常,就必须进行try处理,绝对不能抛出。
*
* @author Qihuan
*
*/
class NegativeNumException extends Exception {
private int value;
public NegativeNumException(String msg,int value) {
// TODO Auto-generated constructor stub
super(msg);
this.value = value;
}
public int getValue() {
return value;
}
}
public class ExceptionPractice {
public int div(int a, int b)throws NegativeNumException,ArithmeticException,ArrayIndexOutOfBoundsException{
int[] arr = new int[a];
System.out.println(arr[4]);
if (b <0) {
throw new NegativeNumException("除数负数异常",b);//手动通过throw关键字抛出一个自定义异常对象。
}
return a/b;
}
public static void main(String[] args) {
ExceptionPractice ep = new ExceptionPractice();
int res = 0;
try {
res = ep.div(5,-1);
} catch (ArithmeticException e) {
System.out.println("0不能做除数");
} catch (ArrayIndexOutOfBoundsException e){
System.out.println("脚标越界异常");
} catch (NegativeNumException e) {
System.out.println("除数出现负数!"+e.getValue());
System.out.println(e.toString());
} finally {//finally中存放的是一定会执行的代码。
System.out.println("FINALLY");
}
System.out.println(res);
System.out.println("Over");
}
}
复制代码
package practice;
/**
* 异常练习:
* 需求:求圆和长方形的面积,针对非法数据,用异常描述。
* @author Qihuan
*
*/
class NoValueException extends RuntimeException {
public NoValueException(String msg) {
// TODO Auto-generated constructor stub
super(msg);
}
}
interface sharp {
double getArea();
}
class Rectangle implements sharp {
private double length, width;
public Rectangle(double length, double width) {
// TODO Auto-generated constructor stub
if (length <= 0 || width <= 0)
throw new NoValueException("长宽非法!");
this.length = length;
this.width = width;
}
@Override
public double getArea() {
// TODO Auto-generated method stub
return length*width;
}
}
class Circle implements sharp {
public static final double PI = 3.14;
private double radius;
public Circle(double radius) {
// TODO Auto-generated constructor stub
if (radius <= 0)
throw new NoValueException("半径非法!");
this.radius = radius;
}
@Override
public double getArea() {
// TODO Auto-generated method stub
return radius*radius*PI;
}
}
public class ExceptionTest {
public static void main(String[] args) {
System.out.println("长方形的面积:");
Rectangle r = new Rectangle(-2, 5);
System.out.println(r.getArea());
System.out.println("圆的面积:");
Circle c = new Circle(0);
System.out.println(c.getArea());
}
}
复制代码
作者:
wuwenwen3779
时间:
2015-7-5 22:07
赞一个喽!!!!!!!!
作者:
hejin67410
时间:
2015-9-21 20:42
谢谢分享
作者:
maxwell247
时间:
2015-9-21 21:02
第二部分的代码写得很精彩。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2