Exception Handling In Web Development 2003-2007 DevelopIntelligence LLC
Presentation Topics What are Exceptions? How are they handled in Java development? JSP Exception Handling mechanisms
What are Exceptions? Those nasty little features 2003-2007 DevelopIntelligence LLC
What are Exceptions? Exceptions represent some form of failure during processing Exceptions are generated by: The Java language library - i.e.: NumberFormatException You - i.e.: MyApplicationException A third-party API - i.e.: SQLException
Exceptions are Objects Exceptions are Objects in Java Exceptions have a class hierarchy Hierarchy creates two broad types Checked by compiler Not-checked by compiler (RuntimeExceptions) Like all objects, Exceptions have: States Behaviors
Handling Exceptions in Java 2003-2007 DevelopIntelligence LLC
Exception Handling in Java Typically three step process Exception is instantiated and customized with error message Exceptions cause flow-control redirection to occur within the JVM through throwing Exceptions are passed by JVM to handler using callback-like mechanism Process relies on special language structure try { //contains potentially faulty code //exception instantiated and thrown here } catch(exception e) { //contains failure recovery code //exception handled or re-thrown here } finally { //performs any final cleanup }
Instantiating an Exception Many different types of Exceptions are available Use normal Object instantiation mechanisms Basic example - Exception e = new NumberFormatException( Invalid number );
Causing Flow-control redirection Need to tell virtual machine of failure Performed by throwing exception Virtual machine assigns stack trace Virtual machine locates and passes exception to handler Basic example (cont.) - throw e;
Callback handling Mechanism Similar to event handling semantics Has detection mechanism for type of exception, specified through argument list Exception object passed as reference value Exception is delivered to only one callback JVM tries to resolve most specific type first JVM may rely on generalized callback if specific callback is unavailable Exception can be delivered to additional callbacks through re-throwing
Basic Example class Fictitious { private int integervalue; } public void setintegervalue(string s) { try { //try to convert the String into a valid integer integervalue = Integer.parseInt(s); //if it succeeded, let's add one to it integervalue++; } catch(numberformatexception nfe) { //if it failed, let's handle the NumberFormatException //and reset the value of integervalue to -1 integervalue = -1; } }
Exception Handling Best-Practices Callback mechanism is not OO Callback mechanism does not automatically log Unfortunately, you may encounter run-time bugs
JSP Exception Handling Mechanisms 2003-2007 DevelopIntelligence LLC
Handling Exceptions in web development Always consider separation of concern Rely on web development frameworks capabilities Attempt, if possible, to reuse exception code Address both application level (SQLException) and server level problems (404 Errors)
JSP Exception Handling Mechanisms Two types of JSP exception handling 1. Java-centric Use familiar Java semantics Addresses application-level errors 2. Container-centric Considered declarative exception handling Address application and server-level erros
Application Level Exceptions Page directive supports two exception related attributes: <%@ page errorpage= error.jsp %> <%@ page iserrorpage= true %> One JSP can be both an errorpage and an iserrorpage
Application Level Exception Example convert.jsp conversionerr.jsp
Server Level Exceptions Server-level exceptions are generated by: Unhandled application-level exception Underlying HTTP web server failure Malformed / invalid client requests Other reasons Container-managed responses configured in web.xml Container forwards request to specified resource Resource (typically JSP) generates response
About DevelopIntelligence Founded in 2003 Provide individual and organizational performance services in the area software development Represents over 35 years of combined experience enabling software development community through educational and performance services Represents over 50 years of combined software development experience Delivered training to over 25,000 developers worldwide
Contact Us For more information about our services, please contact us: Kelby Zorgdrager Kelby@DevelopIntelligence.com 303-395-5340