今天猛然间发现有点不清晰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
}
}
做了以下测试。
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
}
}