学习下excepion

今天猛然间发现有点不清晰excepion。
做了以下测试。
package excepionsTest;

import com.sun.java_cup.internal.Main;

public class test {
public static void main(String[] args) {
try {
throwsTest();
} catch (NullPointerException e) {
System.out.println("111");
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println("ArrayIndexOutOfBoundsException");
}


}

public static void throwsTest() throws NullPointerException{
throw new ArrayIndexOutOfBoundsException();
}
}

记住:
1.方法定义里 使用throws。一般使用办法是自己捕获异常,然后抛出方法中定义的异常。
2.方法内部实现中,使用使用throw。
3.如果方法发生异常,JAVA会上溯到调用这个方法的方法内部,一直到捕获异常为止。
4.新建异常只要继承Exception就行。


package com.gas.gps.ti.terminalcapacity.mapcache;

import com.gas.gps.ti.TargetInterfaceException;

public class SimToCapacityMappingException extends TargetInterfaceException {

private static final long serialVersionUID = 1L;

/**
* 构造器
*/
public SimToCapacityMappingException() {
// TODO Auto-generated constructor stub
}

/**
* 构造器
*
* @param message
*/
public SimToCapacityMappingException(String message) {
super(message);
// TODO Auto-generated constructor stub
}

/**
* 构造器
*
* @param cause
*/
public SimToCapacityMappingException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}

/**
* 构造器
*
* @param message
* @param cause
*/
public SimToCapacityMappingException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}

/**
* FIXME
*
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值