(2001 3 ) from Yongwoo s Park Java Exception Handling Programming from Yongwoo s Park 1
Java Exception Handling Programming from Yongwoo s Park 2
1 4 11 4 4 try/catch 5 try/catch/finally 9 11 12 13 13 13 19 19 Java Exception Handling Programming from Yongwoo s Park 3
from Yongwoo s Park, A A, 0 Divide by 0,,,,,,, try/catch/finally throw,, main main, main, main Java Exception Handling Programming from Yongwoo s Park 4
,, try/catch // catch(exceptiontype-1 exceptionname-n) { //, catch(exceptiontype-2 exceptionname-2) { //, //, catch(exceptiontype-n exceptionname-n) { // try/catch, try, catch try catch try : try try, catch catch, try, try catch, catch,, try,, catch Java Exception Handling Programming from Yongwoo s Park 5
catch : try catch finally catch try, catch try, try catch, try, catch, catch 1 try/catch 1 ClassforName( ), ClassNotFoundException, DriverManagergetConnection( ), SQLException,, try, catch Connection conn = null; Class forname("sunjdbcodbcjdbcodbcdriver"); conn = DriverManagergetConnection("jdbc:odbc:Guestbook", "sa", ""); catch(sqlexception e) { eprintstacktrace(); catch(classnotfoundexception e) { eprintstacktrace(); 1 1 try,, catch, 1, JDBC sunjdbcodbcjdbcodbcdriver 2 ClassNotFoundException, catch 2, ClassNotFoundException catch try/catch,, Java Exception Handling Programming from Yongwoo s Park 6
Systemoutprintln( Exception: + e), 2 eprintstacktrace() Connection conn = null; X ClassforName("sunjdbcodbcJdbcOdbcDriver"); conn = DriverManagergetConnection("jdbc:odbc:Guestbook", "sa", ""); catch(sqlexception e) { eprintstacktrace(); catch(classnotfoundexception e) { eprintstacktrace(); 2 ClassNotFoundException, 1, JDBC,, 3 SQLException, catch catch, try/catch Connection conn = null; ClassforName("sunjdbcodbcJdbcOdbcDriver"); conn = DriverManagergetConnection("jdbc:odbc:Guestbook", "sa", ""); X catch(sqlexception e) { eprintstacktrace(); catch(classnotfoundexception e) { eprintstacktrace(); 3 SQLException, catch, catch, catch Java Exception Handling Programming from Yongwoo s Park 7
, 4 catch, try, try JDBC sunjdbcodbcjdbcodbcdriver ClassNotFoundException ClassNotFoundException ClassNotFoundException catch,, catch, Exception catch Connection conn = null; X ClassforName("sunjdbcodbcJdbcOdbcDriver"); conn = DriverManagergetConnection("jdbc:odbc:Guestbook", "sa", ""); catch(exception e) { eprintstacktrace(); catch(sqlexception e) { eprintstacktrace(); catch(classnotfoundexception e) { eprintstacktrace(); 4 catch, try ClassNotFoundException, 4,, catch, catch, catch,, try catch 5 catch catch Java Exception Handling Programming from Yongwoo s Park 8
Connection conn = null; X ClassforName("sunjdbcodbcJdbcOdbcDriver"); conn = DriverManagergetConnection("jdbc:odbc:Guestbook", "sa", ""); catch(sqlexception e) { eprintstacktrace(); catch(classnotfoundexception e) { eprintstacktrace(); catch(exception e) { eprintstacktrace(); 5 catch try/catch finally try/catch, finally finally try/catch, try catch finally (resources),,, try, catch, finally // catch(exceptiontype-1 exceptionname-n) { //, catch(exceptiontype-2 exceptionname-2) { //, //, catch(exceptiontype-n exceptionname-n) { // Java Exception Handling Programming from Yongwoo s Park 9
finally { // try //, JDBC Connection, Connection close(),?, Connection close(), try,, finally 6 try, try, try finally, finally connclose(), Connection conn = ConnectionPoolcreateConnection(); PreparedStatement pstmt = null; String query = "INSERT INTO x (a, b, c) VALUES (?,?,?)"; pstmt = connpreparestatement(query); pstmtexecuteupdate(); catch(sqlexception e) { eprintstacktrace(); finally { connclose(); 6, 6 try, 7 connpreparestatement( ), SQLException, SQLException catch catch, catch finally finally Java Exception Handling Programming from Yongwoo s Park 10
Connection conn = ConnectionPoolcreateConnection(); PreparedStatement pstmt = null; String query = "INSERT INTO x (a, b, c) VALUES (?,?,?)"; X pstmt = connpreparestatement(query); pstmtexecuteupdate(); catch(sqlexception e) { eprintstacktrace(); finally { connclose(); 7 try/catch, try try/catch, catch try/catch, try try/catch/finally, finally try/catch/finally Connection conn = ConnectionPoolcreateConnection(); PreparedStatement pstmt = null; connsetautocommit(false); String query = "INSERT INTO x (a, b, c) VALUES (?,?,?)"; pstmt = connpreparestatement(query); while() { if(pstmtexecuteupdate() < 1) { throw new SQLException("Insert failed"); conncommit(); catch(sqlexception e) { eprintstacktrace(); connrollback(); catch(sqlexception e1) { e1printstacktrace(); finally { Java Exception Handling Programming from Yongwoo s Park 11
connsetautocommit(true); pstmtclose(); connclose(); catch(sqlexception e1) { e1printstacktrace(); 8 JDBC 8 JDBC,, try/catch/finally, catch catch try/catch, try/catch/finally catch e, catch try/catch catch e e1 catch(sqlexception e) { catch(sqlexception e1) { finally { catch(sqlexception e) { 8, try/catch/finally try connsetautocommit(false), INSERT, ISNERT Java Exception Handling Programming from Yongwoo s Park 12
, INSERT while, while, INSERT conncommit(), finally, connsetautocommit(true), INSERT pstmt close(), conn close(), connsetautocommit(true), pstmtclose(), connclose(), SQLException try/catch, try/catch/finally try INSERT, INSERT, try SQLException catch connrollback(), connrollback(), SQLException try/catch, finally finally,, try, catch, finally, try/catch/finally,,, 9 Java Exception Handling Programming from Yongwoo s Park 13
javalangobject javalangthrowable javalangerror javalangruntimeexception javalangexception Unchecked Exception Checked Exception 9 try/catch Error try/catch Exception, Exception RuntimeException Exception Error: RuntimeException : RuntimeException, Exception RuntimeException : Exception RuntimeException try/catch, ErrorAWTError, LinkageError, ThreadDeath, VirtualMachineError, try/catch, ThreadDeath,, RuntimException, Java Exception Handling Programming from Yongwoo s Park 14
, RuntimException,, NullPointerException new int a[]=null; i = a[0]; Systemoutprintln("a[0]="+ a[0]); // 6 10 NullPointerException,,,,,, ExceptionTest2java main() 6 Exception in thread "main" javalangnullpointerexception: at ExceptionTest2main(ExceptionTest2java:6) 11 NullPointerException,,,, Exception RuntimeException try/catch,,,,,,, try/catch, ClassNotFoundException Java Exception Handling Programming from Yongwoo s Park 15
ClassforName(), SQLException DriverManagergetConnection() connclose(), try/catch ClassforName(jdbcDriver); Connection conn = DriverManagergetConnection(jdbcUrl, "sa", ""); connclose(); 12 try/catch,,, try/catch,, ClassNotFoundException SQLException try/catch, Xjava:9: unreported exception javalangclassnotfoundexception; must be caught or declared to be thrown ClassforName(jdbcDriver); ^ Xjava:10: unreported exception javasqlsqlexception; must be caught or declared to be thrown Connection conn = DriverManagergetConnection(jdbcUrl, "sa", ""); ^ Xjava:13: unreported exception javasqlsqlexception; must be caught or declared to be thrown connclose(); ^ 13 try/catch, ClassNotFoundException SQLException, (CLASSPATH), try/catch Java Exception Handling Programming from Yongwoo s Park 16
ClassforName(jdbcDriver); Connection conn = DriverManagergetConnection(jdbcUrl, "sa", ""); /* */ catch(exception e) { eprintstacktrace(); finally { connclose(); 14 try/catch 12 14 14 try/catch, 15, 14 try/catch, conn try, finally conn, symbol : variable conn location: class X connclose(); ^ 15 cannot resolve symbol, 14 conn try,, Connection conn; ClassforName(jdbcDriver); conn = DriverManagergetConnection(jdbcUrl, "sa", ""); /* */ catch(exception e) { eprintstacktrace(); finally { connclose(); 16 try conn Java Exception Handling Programming from Yongwoo s Park 17
, 16 conn try, 17, conn try, conn try, finally connclose() conn, conn null 17, finally connclose(), Connection close() SQLException, connclose() finally, connclose() try/catch 12, Connection conn=null; ClassforName(jdbcDriver); conn = DriverManagergetConnection(jdbcUrl, "sa", ""); catch(exception e) { eprintstacktrace(); finally { connclose(); catch(sqlexception e) { eprintstacktrace(); 18 try/catch Java Exception Handling Programming from Yongwoo s Park 18
Throwable Throwable,, (Checked Exception),,,,, throws,, ',', protected void service(httpservletrequest req, HttpServletResponse res) throws ServletException, javaioioexception { 19 19 service() ServletException javaioioexception ( ), service(), throws, throw,,, throw javalangthrowable, throw try/catch Java Exception Handling Programming from Yongwoo s Park 19
throws,, public void delete(long seqnum) throws SQLException { deletepstmtsetint(1, (int)seqnum); if(deletepstmtexecuteupdate() < 1) { throw new SQLException("Delete failed"); 20 20, delete() throws SQLException,, throw, throw new SQLException("Delete failed");, throws,, try/catch throws catch throw, throws, throws, throws throws, throws, throws Java Exception Handling Programming from Yongwoo s Park 20
throws, Java Exception Handling Programming from Yongwoo s Park 21