自定义Java异常
1.前言:
你的程序总有一天会崩溃掉,在崩溃掉的时候我们要知道它在哪,为了什么而崩溃掉,数据的保存或者丢失情况如何等问题。我们可以通过继承类java.lang.Throwable的子类:Exception来设计我们自己的Java异常。Exception类用于描述程序能够捕获的异常,如ClassNotFoundException。要注意的是自定义异常类之间也可以有继承关系,同时也需要为自定义异常类设计构造方法,以方便构造自定义异常对象。
2.设计实例分析:
这是个比较完整的自定义异常类的设计,其实是比较模板化的东西。
package playground;
import java.io.*;
public class MyException extends Exception {
private int id; // a unique id
private String classname; // the name of the class
private String method; // the name of the method
private String message; // a detailed message
private MyException previous =
null; // the exception which was caught
private String separator = "\n"; // line separator
public MyException(int id, String classname, String method,
String message, MyException previous) {
this.id = id;
this.classname = classname;
this.method = method;
this.message = message;
this.previous = previous;
}
public String traceBack() {
return traceBack("\n");
}
public String traceBack(String sep) {
this.separator = sep;
int level = 0;
MyException e = this;
String text = line("Calling sequence (top to bottom)");
while (e != null) {
level++;
text += line("--level " + level + "--------------------------------------");
text += line("Class/Method: " + e.classname + "/" + e.method);
 
相关文档:
Java开发中,最终的程序发布一般是要打成jar包形式的,而有一些配置文件是放在jar里面的,这样在做File形式的读取时,就会发现读不到相应的文件。下面提供一种解决方式:String fileName = "/config/abcd.config";
InputStream in = getClass().getResourceAsStream(fileName);
BufferedReader br = null;
try{
br = n ......
先看一段代码
public class Hello{
public static void main(String[] args){
int i = 5 , j = 2;
System.out.println(i+j);
......
两个tomcat Java定时任务的例子
例一:
本例依据Java自身提供的接口实现,通过监听器(Listener)和定时器(Timer)定时执行某个任务(Task)。
专业的开源工具可参考Quartz:http://www.opensymphony.com/quartz/
MyListener:
import
java.util.Timer;
import
javax.servlet.ServletContextEvent;
i ......
<!--
/* Font Definitions */
@font-face
{font-family:SimSun;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:宋体;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:"\@SimSun&qu ......
package fileIo;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
public&nb ......