就是自定义异常,创建了这个异常类以后就可以直接调用这个异常了,
拜托了
拜托了
解决方案 »
- 打印出2--1000的完全数
- RandomAccessFile
- 大家好! 我想请教一下 java swing 能出widget (鱼鱼秀)的东东吗?有会的 请赐教!
- 在UltraEdit的左侧树中删了文件后,回车站里没有,有办法恢复码?
- IMPORT 自己写的类问题,急,在线等....
- 如何让一个JFrame弹出后,出现在窗口的最前面呢?
- 写rmi程序时,security policy是怎么回事?
- 如何让jsp或servlet进入wait状态
- 哪位好心的大哥用过websphere,指点一下基础问题,多谢!!!
- java 的简单配置问题
- 关于Java中的Random类,熟悉的进
- 用JAVA如何将XML文件中的一个值存入String对象??
public MyException() {}
public MyException(String message) {
super(message);
}
}
不明白LZ什么意思,自定义异常不就是这样吗
import java.lang.reflect.*;class DoubleException extends Exception{
public String toString(){
return "It's not a double value!";
}
}public class IsDouble{
double d = 1.0;
int j = 1;
public static void main(String args[]){
try{
Class c = Class.forName("IsDouble");
Field fieldlist[] = c.getDeclaredFields(); for (int i = 0; i < fieldlist.length; i++) {
Field fld = fieldlist[i];
if(fld.getType().getName().equals("double"))
throw new DoubleException();
else
System.out.println(fld.getName() +": "+ fld.getType());
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
输出:
It's not a double value!
at IsDouble.main(IsDouble.java:22)
double d = 1.0;
Double d = new Double(d);
if(d instanceof Double) throw new DoubleException();
public static boolean isDou(Number n){
boolean b=false;
try {
b=Class.forName("java.lang.Double").isInstance(n);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(!b){
try {
throw new MyException("is not a Double");
} catch (MyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return b;
}
public static void main(String[] args){
Double d=1.0;
int i=1;
System.out.println(isDou(d));
System.out.println(isDou(i));
}
}
测试结果:true
day05.MyException: is not a Double
at day05.IsDouble.isDou(IsDouble.java:14)
at day05.IsDouble.main(IsDouble.java:26)
false
就用我1楼的那个异常
非受检异常!
public class MyException extends RuntimeException{
private static final long serialVersionUID = -1663367068727673091L; public DAOException() {
super();
// TODO Auto-generated constructor stub
} public DAOException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
} public DAOException(String message) {
super(message);
// TODO Auto-generated constructor stub
} public DAOException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
}
{
private static final long serialVersionUID = 1L; public String toString(){
return "It's not a double value!";
}
}public class IsDouble{
double d = 1.0;
int j = 1;
public static void main(String args[]){
try{
Class<?> c = Class.forName("IsDouble");
Field fieldlist[] = c.getDeclaredFields(); for (int i = 0; i < fieldlist.length; i++) {
Field fld = fieldlist[i];
if(fld.getType().getName().equals("double"))
System.out.println(fld.getName() +": "+ fld.getType());
else
throw new DoubleException();
}
}catch (Exception e) {
e.printStackTrace();
}
}
}这样才能判断类中所有的变量是不是double类型