Java 中构建自己的异常类
2006-05-20 11:18
429 查看
创建testc.java文件:
package com.test.my;
public class testc {
/**
* @param args
*/
public static void main(String[] args) {
try {
testException();
} catch (Exception e) {
System.out.println(e.toString());
}
}
public static String testException() throws Exception {
if (true) throw new MyException("异常!");
else return "";
}
}
创建MyException.java文件:
/**
*
*/
package com.test.my;
/**
* @author 苏显斌
*
*/
public class MyException extends Exception{
private static final long serialVersionUID = -3076697639889780533L;
public MyException() {
super("我的异常!");
}
public MyException(String msg) {
super(msg);
}
public String toString() {
return super.toString();
}
}
异常处理需要注意的一些问题:
1、在进行异常处理的时候,注意catch异常的优先级,比如最先应当捕获那个异常放在最前面的catch里面,依次类推,最后是Exception异常;
2、finally内只能进行一些清理工作,不要在里面进行赋值之类的操作,比如看下面的例子,结果和我们想像的会有一些冲突:
package com.test.my;
public class testc {
/**
* @param args
*/
public static void main(String[] args) {
try {
String result = testException();
System.out.println(result);
} catch (Exception e) {
System.out.println(e.toString());
}
}
public static String testException() throws Exception {
String result = "";
try {
// if (true) throw new MyException("异常!");
int i = 0;
int b = 10 / i;
} finally {
result = "正常!";
}
return result;
}
}
得到的结果是:java.lang.ArithmeticException: / by zero
package com.test.my;
public class testc {
/**
* @param args
*/
public static void main(String[] args) {
try {
testException();
} catch (Exception e) {
System.out.println(e.toString());
}
}
public static String testException() throws Exception {
if (true) throw new MyException("异常!");
else return "";
}
}
创建MyException.java文件:
/**
*
*/
package com.test.my;
/**
* @author 苏显斌
*
*/
public class MyException extends Exception{
private static final long serialVersionUID = -3076697639889780533L;
public MyException() {
super("我的异常!");
}
public MyException(String msg) {
super(msg);
}
public String toString() {
return super.toString();
}
}
异常处理需要注意的一些问题:
1、在进行异常处理的时候,注意catch异常的优先级,比如最先应当捕获那个异常放在最前面的catch里面,依次类推,最后是Exception异常;
2、finally内只能进行一些清理工作,不要在里面进行赋值之类的操作,比如看下面的例子,结果和我们想像的会有一些冲突:
package com.test.my;
public class testc {
/**
* @param args
*/
public static void main(String[] args) {
try {
String result = testException();
System.out.println(result);
} catch (Exception e) {
System.out.println(e.toString());
}
}
public static String testException() throws Exception {
String result = "";
try {
// if (true) throw new MyException("异常!");
int i = 0;
int b = 10 / i;
} finally {
result = "正常!";
}
return result;
}
}
得到的结果是:java.lang.ArithmeticException: / by zero
相关文章推荐
- Java 中构建自己的异常类(二)
- [学习笔记]Java代码构建一个线程池的自己学习写的实例,用这个你会更好的理解文章内容
- 构建自己的Java Web框架(二)之一个Filter引发的血案
- 每天一点儿java---继承exception类来实现自己的异常类
- java中异常的一些自己的理解
- 【java笔记】Java中定义自己的异常类
- 构建自己的监测器【4】-java -D参数使用
- 在java里面怎么定义异常类用于自己编写的类的异常处理?
- 构建自己的Java并发模型框架
- 构建自己的Java Web框架(三)之JAVA反射机制
- 构建自己的监测器【7】-Java Interactive Profiler 翻译文档
- Java Service Wrapper2-构建自己的Wrapper应用
- IDEA 使用Gradle构建项目后没有src文件,自己新建后不能创建Java文件的解决办法
- Java Service Wrapper-构建自己的Wrapper应用
- 手动利用Apache Ant构建部署自己的Java项目
- Java异常封装(自己定义错误码和描述,附源码)
- 每天一点点java---继承exception类来实现自己的异常类
- 构建自己的Java并发模型框架
- 构建自己的基于 Java 的超级计算机