异常定义
异常(Exception) 是正常程序流程所不能处理或没有处理的异常情况或异常事件
异常类型
异常 | 说明 |
---|---|
ArithmeticException | 算术错误,如除以0 |
IllegalArgumentException | 方法收到非法参数 |
ArrayIndexOutOfBoundsException | 数组下标出界 |
NumberFormatException | 字符串转化为数字异常 |
ClassNotFoundException | 不能加载请求的类 |
IOException | I/O异常的根类 |
FileNotFouondException | 不能找到文件 |
EOFException | 文件结束 |
IllegalAccessException | 对类的访问被拒绝 |
NoSuchMethodException | 请求的方法不存在 |
InterruptedException | 线程中断 |
Exception类
Exception类是java.lang.Throwable类的一个子类
构造函数为:
public Exception();
public Exception(String s);
常用方法为:
public String toString();返回异常类信息
如何进行异常处理
- java异常处理机制
- 使用throws声明异常
- 使用throw抛出异常
- 使用try/catch处理异常,finally无论是否发生异常代码总能执行
try/catch块
public class Test {
public static void go(int a) throws Exception{
if (a<10&&a>0) {
System.out.println(a);
}else{
throw new Exception("数字大小不合法");
}
}
public static void main(String[] args) {
try {
go(12);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
try/catch/finally
public class ExceptionTest1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入除数:");
int a=scanner.nextInt();
try {
int b;
b=12/a;
} catch (ArithmeticException e) {
System.out.println("除数为零;");
}finally {
System.out.println("这一个部分不论有没有异常都可以实现");
}
}
}
这是一个比较好的例子
注意当为yes,为no的时候各自会输出那些语句从而更加深刻的理解异常
注意这里有自己定义的的异常类,所以必须要自己定义自己的异常类
public class TestException {
static void doRisky(String test) throws ScaryException{
System.out.println("start test");
if ("yes".equals(test)) {
throw new ScaryException();
}
System.out.println("end risk");
return;
}
public static void main(String[] args) {
String test="yes";
try {
System.out.println("start try");
doRisky(test);
System.out.println("end try");
} catch (ScaryException se) {
System.out.println("scary Exception");
}finally {
System.out.println("finally!");
}
System.out.println("end of main");
}
}
多重catch块
因为异常也是类,所以这里也有多态相关还有子类异常父类异常
这里有很多的都与前面的多态很有引导性,还是比较复杂所以后面会继续补充,这里暂时空着
自定义异常类
//定义一个phone类里面的setprice方法限定值,不可以为负值和大于10000
//自定义两个异常的类
class NeException extends Exception{
public NeException(){
System.out.println("数值不可以为负!");
}
public NeException(String message){
super(message);
System.out.println(getMessage());
}
}
public class overException extends Exception{
public overException() {
System.out.println("数值大于10000了");
}
public overException(String message) {
super(message);
System.out.println(getMessage());
}
}
public class Phone {
private long price;
/**
* @param price
*/
public Phone() {
}
public Phone(long price) {
super();
this.price = price;
}
public long getPrice() {
return price;
}
public void setPrice(int price) throws NeException,overException {
if (price<0) {
throw new NeException();
//这里也可以直接调用自定义的含有String参数的构造函数
//throw new NeException("不可以为负值");
}
else if (price>10000) {
throw new overException();
}
else {
this.price=price;
}
}
}
public class Test1 {
public static void main(String[] args) {
Phone phone= new Phone();
try {
phone.setPrice(-1);
} catch (overException e) {
e.getMessage();
}catch (NeException e) {
e.getMessage();
}
}
}
此文章还不完整后面会继续补充