各位,我原打算这样的,请看代码! public class MyException extends Exception { private static boolean bDebugMode = true; private int nErrorCode; /** * print debug information. * @param strInfo debug information * @return void **/ private void outputDebugInfo(String strInfo) { if (bDebugMode) { MyDebug.print("MyException--->" + strInfo); } }
/** * Constructs a new MyException with the specified error code. * @param strReason error reason * @param nErrorCode error code * @see java.lang.Throwable#Throwable(String strMessage) **/ public MyException(String strReason, int nErrorCode) { super(strReason); this.nErrorCode = nErrorCode; outputDebugInfo("MyException----->" + strReason); }
/* * Returns the error code of this object. * @return error code **/ public int getErrorCode() { return this.nErrorCode; } } 在这个类里,我只能实现错误代码和信息属性的表示,对于LOG的处理还得依赖MyDebug或者MyLog(在此还没有写)进行分级,我的错误码统一归MyErrorCode类管理,只是一些错误常量,我对于Exception的分派和系统回调功能,总是想在这个类Exception里实现机能封装,请帮我出出主意,另外请问我这样作有什么缺点。拜托了,各位。
嗯,分派是指分类吗?用继承不行吗? public class HighlevelException extends Exception {...} public class Detailed1Exception extends HighlevelException {...} public class Detailed2Exception extends HighlevelException {...}如果你说得系统回调是指重新运行出现exception的部分,这好像不能在exception中实现,需要你在程序中实现。
public class MyException extends Exception
{
private static boolean bDebugMode = true;
private int nErrorCode;
/**
* print debug information.
* @param strInfo debug information
* @return void
**/
private void outputDebugInfo(String strInfo)
{
if (bDebugMode)
{
MyDebug.print("MyException--->" + strInfo); }
}
/**
* Constructs a new MyException with the specified error code.
* @param strReason error reason
* @param nErrorCode error code
* @see java.lang.Throwable#Throwable(String strMessage)
**/
public MyException(String strReason, int nErrorCode)
{
super(strReason);
this.nErrorCode = nErrorCode;
outputDebugInfo("MyException----->" + strReason);
}
/*
* Returns the error code of this object.
* @return error code
**/
public int getErrorCode()
{
return this.nErrorCode;
}
}
在这个类里,我只能实现错误代码和信息属性的表示,对于LOG的处理还得依赖MyDebug或者MyLog(在此还没有写)进行分级,我的错误码统一归MyErrorCode类管理,只是一些错误常量,我对于Exception的分派和系统回调功能,总是想在这个类Exception里实现机能封装,请帮我出出主意,另外请问我这样作有什么缺点。拜托了,各位。
Exception的分派的意思就是,我对于我所设计的系统内所有抛出的Exception能进行管理以及后续的分派处理,通过设计的细化,实现对Exception的分级,用来了解对商务系统的影响和当前商务系统的状态以及决定后续的处理。
系统的回调: 这是我的想法,因为有些致命Exception抛出以后,我不想失去处理前的信息,类似于一个恢复dump的方式,实现商务系统的重新动作。
public class HighlevelException extends Exception {...}
public class Detailed1Exception extends HighlevelException {...}
public class Detailed2Exception extends HighlevelException {...}如果你说得系统回调是指重新运行出现exception的部分,这好像不能在exception中实现,需要你在程序中实现。
jimjxr(宝宝猫)兄:你好!我的想法是为了我区分和处理Exception用,请在我怎么定义我的Exception的原型,以及怎么处理这些Exception,这方面给一些经验。比如使用接口,继承,分派。也请大家关注了,因为我每做完一个项目,回头看Exception处理时,总觉得单纯的错误码很不好,扩充时需要动错误码的静态变量,有时客户意见很大的,另外很多时候Exception的级别不一样,必须得catch一个,处理,再抛出,再循环,我觉得这肯定是设计的问题。
catch时你不能一个一个处理吗,如:
try
{
...
}
catch( Detailed1Exception de1 )
{
...
}
catch( Detailed2Exception de2 )
{
...
}
有关分派,具体有没有人作过dispatcher的构造器,我想看看是什么样子的,希望大家捧捧场。
还有就是在Exception类里,怎样运用Template method。