java异常应用_Java 异常处理及应用

Java

JavaBy oldface2009/03/11

JavaJavaJava

Javajava.lang.ThrowableJava APIErrorExceptionJava 1Java

1 JavaThorwableErrorException

Exception(RuntimeException)Unchecked ExceptionChecked Exception

1ErrorException

ErrorOutOfMemoryErrorThreadDeathJavaJVM

Exception2

RuntimeExceptionNullPointerException IndexOutOfBoundsException

RuntimeExceptionExceptionIOExceptionSQLExceptionExceptionJava

Javatrycatchfinallythrowthrows1javatry {// ()} catch () {// } finally {// }

trytrycatchtrycatchThrowable trycatchcatchcatchcatchcatch JVM

finallycatchtryJava

2trycatchfinally

trycatchfinally.trycatchfinally.trycatchfinallycatchfinallyfinallycatch

tryfinally.

trytrycatchfinally

trycatch,Javacatchcatchcatch

try...catch.throws

throw,.3throwthrows

throwThrowableJVMErrorRuntimeExceptionthrowscatch

public static void main() throws Exception {// throw new Exception(Exception");}

4Throwable

getCause() cause null

getMessage()

printStackTrace() System.err RuntimeExceptionRuntimeException1

JavaThrowableThrowableSQLException DAOException

DAOException

public class DAOException extends RuntimeException { // ()public DAOException(String message, Throwable cause) { super(message, cause);}}SQLExceptioneDAOException

DAOException daoException = new DAOException ( "SQL", e);

Throwable,ErrorExceptionRuntimeException22

""ExceptionErrorRuntimeException

1ErrorExceptionSpring WEBorg.springframework.web.servlet.DispatcherServletdoDispatch()NestedServletException Error2ExceptionRuntimeExceptionRuntimeException

3ErrorRuntimeException2

Java APItry {lowLevelOp();} catch (LowLevelException le) { throw (HighLevelException) new HighLevelException().initCause(le); }legetCause()Java

NestedRuntimeException NestedRuntimeExceptionRuntimeExceptionNestedRuntimeExceptionNestedRuntimeException

3 3 NestedRuntimeException""

NestedRuntimeExceptionNestedRuntimeExceptionNestedRuntimeException AppDAOExceptionOtherException SQLExceptionHibernateExceptionAppDAOExceptionSpring,Spring org.springframework.core.NestedRuntimeException RuntimeExceptionSpringNestedRuntimeExceptionJava

JavaJavaErrorExceptionErrorException

JavaJava APIJava APIJavaJVM

.Java..

.

.Javatry...catch

throws

JavaJavacatch.catchcatch;JavacatchJava.

(1)printStackTtace().

(2)(main()).

finally:

throws:

throws

throw:Javatry..catchJava;Java.Javatry...catch..

try...catch...finallyreturnSystem.exit()

(1) finallySystem.exit()java.lang.Systemexit()JavaJavaJava.returnSystem.exit(2) return.trycatchreturnfinallyfinally

(3) finallyreturnfinallyreturn.(4)finallyreturn

1trycatchreturn

2finallyreturntrycatchthrowbooleantruefalseJava

voidboolean

booleanlogin(Stringusername,Stringpassword);

booleantrueloginfalseint012

intlogin(Stringusername,Stringpassword);

ifelse

intlogon=UserManager.login(xx, xx);

if(logon==0) {...}elseif(logon==1) {...}elseif(logon==2) {..}

Userlogin(Stringusername,Stringpassword);throwsUserNotFoundException,PasswordNotMatchException;

try {userService.login(xx, xx);....// } catch(NoSuchUserExceptione) {.// }catch(PasswordNotMatchExceptione) {....// forward}

UseCasentrycatchExceptionExceptionJava

ExceptionException1UserExistedExceptioncatchif elseOOPtry catch ntry catchif else

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值