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