Python Java Checking for Errors

track_errors = On; I f you change t he track_errors set t ing and youre using PHP as an Apache m odule, youll need t o rest art Apache t o m ake t he change t ake effect .

2.3.6 Python

Pyt hon program s signal errors by raising except ions, and handle errors by cat ching except ions in an except block. To obt ain MySQL- specific error inform at ion, nam e an except ion class and provide a variable t o receive t he inform at ion. Heres an exam ple: try: conn = MySQLdb.connect db = cookbook, host = localhost, user = cbuser, passwd = cbpass print Connected except MySQLdb.Error, e: print Cannot connect to server print Error code:, e.args[0] print Error message:, e.args[1] sys.exit 1 I f an except ion occurs, t he first and second elem ent s of e.args will be set t o t he num eric error code and descript ive error m essage, respect ively. Not e t hat t he Error class is accessed t hrough t he MySQLdb driver m odule nam e.

2.3.7 Java

Java program s handle errors by cat ching except ions. I f you sim ply want t o do t he m inim um am ount of work, print a st ack t race t o inform t he user where t he problem lies: catch Exception e { e.printStackTrace ; } The st ack t race shows t he locat ion of t he problem , but not necessarily what t he problem is. I t m ay not be all t hat m eaningful except t o you, t he program s developer. To be m ore specific, you can print t he error m essage and code associat ed wit h an except ion: • All Exception obj ect s support t he getMessage m et hod. JDBC m et hods m ay t hrow except ions using SQLException obj ect s; t hese are like Exception obj ect s but also support getErrorCode and getSQLState m et hods. • For MySQL errors, getErrorCode and getMessage ret urn t he num eric error code and descript ive error st ring. • getSQLState ret urns a st ring t hat provides error values defined according t o t he XOPEN SQL specificat ion which you m ay or m ay not find useful . • You can also get inform at ion about non- fat al warnings, which som e m et hods generat e using SQLWarning obj ect s. SQLWarning is a subclass of SQLException , but warnings are accum ulat ed in a list rat her t han t hrown im m ediat ely, so t hey dont int errupt your program and you can print t hem at your leisure. The following exam ple program , Error.j ava, dem onst rat es how t o access error m essages by print ing all t he error inform at ion it can get it s hands on. I t at t em pt s t o connect t o t he MySQL server and print s except ion inform at ion if t he at t em pt fails. Then it issues a query and print s except ion and warning inform at ion if t he query fails: Error.java - demonstrate MySQL error-handling import java.sql.; public class Error { public static void main String[ ] args { Connection conn = null; String url = jdbc:mysql:localhostcookbook; String userName = cbuser; String password = cbpass; try { Class.forName com.mysql.jdbc.Driver.newInstance ; conn = DriverManager.getConnection url, userName, password; System.out.println Connected; tryQuery conn; issue a query } catch Exception e { System.err.println Cannot connect to server; System.err.println e; if e instanceof SQLException JDBC-specific exception? { print general message plus any database-specific message note how e is cast from Exception to SQLException to access the SQLException-specific methods System.err.println SQLException: + e.getMessage ; System.err.println SQLState: + SQLException e.getSQLState ; System.err.println VendorCode: + SQLException e.getErrorCode ; } } finally { if conn = null { try { conn.close ; System.out.println Disconnected; } catch SQLException e { print general message plus any database-specific message System.err.println SQLException: + e.getMessage ; System.err.println SQLState: + e.getSQLState ; System.err.println VendorCode: + e.getErrorCode ; } } } } public static void tryQuery Connection conn { Statement s = null; try { issue a simple query s = conn.createStatement ; s.execute USE cookbook; s.close ; print any accumulated warnings SQLWarning w = conn.getWarnings ; while w = null { System.err.println SQLWarning: + w.getMessage ; System.err.println SQLState: + w.getSQLState ; System.err.println VendorCode: + w.getErrorCode ; w = w.getNextWarning ; } } catch SQLException e { print general message plus any database-specific message System.err.println SQLException: + e.getMessage ; System.err.println SQLState: + e.getSQLState ; System.err.println VendorCode: + e.getErrorCode ; } } }

2.4 Writing Library Files