设计模式Exception设计
1。Java异常分类
Java异常顶层是Throwable,下来分为了Exception和Error,这个是JDK源码包的范畴,不代表第三方包的规则,Throwable
TheThrowableclassisthesuperclassofallerrorsandexceptionsintheJavalanguage。
Throwable是Java语言异常的超类
Forthepurposesofcompiletimecheckingofexceptions,ThrowableandanysubclassofThrowablethatisnotalsoasubclassofeitherRuntimeExceptionorErrorareregardedascheckedexceptions。
为了在编译时检查异常,Throwable和任何未被申明RuntimeException或Error的子类的Throwable子类都被视为被检查的异常对象。
Error
其中Error更多的表现为JVM内部异常,可以称之为系统异常,为Java程序不可处理的异常,不可用trycatch语句抓取的,比如常见的StackOverflowError(栈内存溢出)和OutOfMemoryError(堆内存溢出)这两种很常见,其他的还有很多,Exception
这个就是我们常见的异常的超级父类了,他为程序可以抓取的异常,此时可能表现在
TheclassExceptionandanysubclassesthatarenotalsosubclassesofRuntimeExceptionarecheckedexceptions。
程序会自动检测非RuntimeException的Exception的子类,并且提示错误
Checkedexceptionsneedtobedeclaredinamethodorconstructorsthrowsclauseiftheycanbethrownbytheexecutionofthemethodorconstructorandpropagateoutsidethemethodorconstructorboundary。
这些异常必须在方法上或者构造方法上用throws声明抛出去,
以上就是我们常见的两大父类对象2。RuntimeException
RuntimeExceptionisthesuperclassofthoseexceptionsthatcanbethrownduringthenormaloperationoftheJavaVirtualMachine。RuntimeExceptionanditssubclassesareuncheckedexceptions。Uncheckedexceptionsdonotneedtobedeclaredinamethodorconstructorsthrowsclauseiftheycanbethrownbytheexecutionofthemethodorconstructorandpropagateoutsidethemethodorconstructorboundary。
RuntimeException是在JVM正常运行期间可以抛出的异常的超类。
RuntimeException及其子类是未检查异常。
未检查的异常不需要在方法或构造函数的抛出子句中声明,如果它们可以由方法或构造函数的执行抛出并传播到方法或构造函数边界之外。publicstaticvoidmain(String〔〕args){try{testError();}catch(Exceptione){System。out。println(e。getMessage());会输出error}}staticvoidtestError()throwsException{error();}staticvoiderror(){try{inti10;}catch(Exceptione){thrownewRuntimeException(error);}}
异常会具有传递性,类似于trace一样,出现后会找到你throws那里,所以不怕断链,3。构造方法protectedThrowable(Stringmessage,Throwablecause,booleanenableSuppression,booleanwritableStackTrace)Constructsanewthrowablewiththespecifieddetailmessage,cause,suppressionenabledordisabled,andwritablestacktraceenabledordisabled。Ifsuppressionisdisabled,getSuppressedforthisobjectwillreturnazerolengtharrayandcallstoaddSuppressedthatwouldotherwiseappendanexceptiontothesuppressedlistwillhavenoeffect。Ifthewritablestacktraceisfalse,thisconstructorwillnotcallfillInStackTrace(),anullwillbewrittentothestackTracefield,andsubsequentcallstofillInStackTraceandsetStackTrace(StackTraceElement〔〕)willnotsetthestacktrace。Ifthewritablestacktraceisfalse,getStackTracewillreturnazerolengtharray。
NotethattheotherconstructorsofThrowabletreatsuppressionasbeingenabledandthestacktraceasbeingwritable。SubclassesofThrowableshoulddocumentanyconditionsunderwhichsuppressionisdisabledanddocumentconditionsunderwhichthestacktraceisnotwritable。Disablingofsuppressionshouldonlyoccurinexceptionalcircumstanceswherespecialrequirementsexist,suchasavirtualmachinereusingexceptionobjectsunderlowmemorysituations。Circumstanceswhereagivenexceptionobjectisrepeatedlycaughtandrethrown,suchastoimplementcontrolflowbetweentwosubsystems,isanothersituationwhereimmutablethrowableobjectswouldbeappropriate。
主要是知道writableStackTrace当设置为true的时候会追踪到出现异常的源头,而不是我们手动抛出的地方
默认是false。4。设计异常
一般来说,我们的最内部的核心接口(或者抽象方法)最好继承与Exception,如果外部我们已经知道我们自定的异常了,那么内部处理的异常可以使用RuntimeException,
对于用户不知道的操作,最好申明异常,对于接口的定义全部要throws抛出异常,让使用者来处理,而不是你来处理,对于程序停止的,异常对于程序有正常运行有影响需要使用e。printstack,最好打印出去,然后使用Runtime。getRuntime()。hook或者exit都可以强制停止,5。总结
对于异常设计上,一般对于我们比如说没人帮我们去抓取异常,此时就用throws,