Error Handling in Java Servlets

Slides:



Advertisements
Similar presentations
Chapter 6 Server-side Programming: Java Servlets
Advertisements

Chapter 17 Failures and exceptions. This chapter discusses n Failure. n The meaning of system failure. n Causes of failure. n Handling failure. n Exception.
Topics Introduction Types of Errors Exceptions Exception Handling
Detecting Bugs Using Assertions Ben Scribner. Defining the Problem  Bugs exist  Unexpected errors happen Hardware failures Loss of data Data may exist.
Exceptions: when things go wrong. Various sources of error public static doSomething() { int i = 3.0; while(!done); { int i = false } ) Syntactic errors.
Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 16: Exception.
Exceptions Ensuring program reliability. Program correctness The term program correctness refers to a program’s working as advertised; that is, it produces.
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
SE-1020 Dr. Mark L. Hornick 1 Exceptions and Exception Handling.
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
CSE 219 COMPUTER SCIENCE III PROPERTIES OF HIGH QUALITY SOFTWARE.
Java for enterprise networks Version 2.3 Feb 2008 JSP Validation and Exception handling Why validate? Client side validation.
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
Chapter 12: Exception Handling
CSCI 6962: Server-side Design and Programming Validation Tools in Java Server Faces.
Chapter 17 - Deploying Java Applications on the Web1 Chapter 17 Deploying Java Applications on the Web.
Handling Exceptions in java. Exception handling blocks try { body-code } catch (exception-classname variable-name) { handler-code }
Chapter 3 Servlet Basics. 1.Recall the Servlet Role 2.Basic Servlet Structure 3.A simple servlet that generates plain text 4.A servlet that generates.
1 Software Construction and Evolution - CSSE 375 Exception Handling - Principles Steve Chenoweth, RHIT Above – Exception handling on the ENIAC. From
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
Karlstad University Computer Science Design Contracts and Error Management External and internal errors and their contracts.
Chapter 8-Exception Handling/ Robust Programming.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Exception and Exception Handling. Exception An abnormal event that is likely to happen during program is execution Computer could run out of memory Calling.
Section 3.3 Exceptional Situations. 3.3 Exceptional Situations Exceptional situation Associated with an unusual, sometimes unpredictable event, detectable.
CSE 1201 Object Oriented Programming
Lec.11 (Chapter 11) Exception Jiang (Jen) ZHENG July 13 th, 2005.
CS122B: Projects in Databases and Web Applications Spring 2017
CS122B: Projects in Databases and Web Applications Winter 2017
Java Exceptions a quick review….
Exceptions Exceptions are used to signal that an unexpected event has happened in a program C++ will generate exceptions for some errors in the program.
Java Programming Fifth Edition
Handling Errors in Web Applications
Web Development Web Servers.
Chapter 10 – Exception Handling
OBJECT ORIENTED PROGRAMMING II LECTURE 10 GEORGE KOUTSOGIANNAKIS
Testing and Debugging.
Exceptions The Need for Exceptions Throwing Exceptions
Software Testing and Maintenance Introduction
Chapter 14: Exception Handling
Design and Maintenance of Web Applications in J2EE
Exceptions 10-Nov-18.
EE422C Software Implementation II
SWE 332 Last Modified Spring 2010 Paul Ammann
Generating the Server Response: HTTP Status Codes
Using Servlet Contexts to Deploy Servlets
CS122B: Projects in Databases and Web Applications Winter 2018
Exceptions Problems in a Java program may cause exceptions or errors representing unusual or invalid processing. An exception is an object that defines.
Exception Handling in Java
Exception Handling and Reading / Writing Files
CS122B: Projects in Databases and Web Applications Spring 2018
Handling State in Web Applications
Object-Oriented Programming Using C++ Second Edition
Java Exceptions Dan Fleck CS211.
CMSC 202 Exceptions 2nd Lecture.
CSC 143 Java Errors and Exceptions.
Pre-assessment Questions
Exceptions 10-May-19.
Debugging and Handling Exceptions
Computer Science 340 Software Design & Testing
CMSC 202 Exceptions 2nd Lecture.
Exception Handling and Event Handling
Java Programming: From Problem Analysis to Program Design, 4e
Input Data Validation for Web Applications
Presentation transcript:

Error Handling in Java Servlets Jeff Offutt http://www.cs.gmu.edu/~offutt/ SWE 642 Software Engineering for the World Wide Web sources: Professional Java Server Programming, Patzer, Wrox

Six Major Quality Attributes Reliability : Decreased by unhandled failures; the software crashes more often Usability : Poor error handling decreases usability Security : Failures lead to security holes Availability : Not available when software crashes Scalability : As sites grow, unhandled exceptions are more likely to lead to failures Maintainability : Exception handling increases complexity and difficulty to maintain 6/5/2019 © Offutt

Web Site Software Failures Failures happen : Faults in programs Invalid input data Data base problems Hardware problems Network problems Successful web sites must handle errors! Java provides rich error handling constructs 6/5/2019 © Offutt

Error Handling Computer Science courses typically ignore error handling Advanced software engineering (SWE 619) pays little attention to it It’s considered boring ! Effective web software engineers pay a lot of attention to error handling 6/5/2019 © Offutt

Seven Steps for Error Handling When errors occur, servlet should: Recognize the problem Diagnose the problem Handle the problem (if possible) Inform the user (if necessary) Inform the administrator (if necessary) Log details of the problem Repeat service (if productively possible) 6/5/2019 © Offutt

Error Handling – What & Why These are crucial things the engineer should know The hard part : Anticipating what might go wrong! 6/5/2019 © Offutt

Java Exceptions When a run-time failure is detected : An exception is thrown to the program The program can handle it Programs can also explicitly throw exceptions java.lang.Exception Exceptions are objects : Exception objects Methods must list the exceptions they can throw (throws) 6/5/2019 © Offutt

Java Exceptions (2) Rule : If a method p() calls another method m() that throws an exception e, p() must either : handle e, or throws e (that is, as part of the method signature) Hints (usability issues — SWE 632) : Try to handle exceptions low, not high Try to handle exceptions without informing users If users enter wrong data, only make them re-enter the data that was wrong Make error messages polite, non-accusing, explicit, and be sure to let users know how to correct the problem Never send exceptions back to the user Messages are not intended for users Response object is not sent 6/5/2019 © Offutt

Handling Exceptions Exceptions are thrown and caught try { // statements that can throw exceptions, including method // calls, must be in try blocks } catch (NumberFormatException e) do stuff … catch (Exception e) // Generic exception, handles all others // from the exception base class. 6/5/2019 © Offutt

Handling Exceptions (2) Typical exception methods : String toString() – returns name + error message Of questionable usefulness to user. void printStackTrace() – sequence of method calls Send to log or sysadm, NEVER to user! String getMessage() – error message in toString() 6/5/2019 © Offutt

Look at LoanCalculator example Exceptions Examples Look at LoanCalculator example http://cs.gmu.edu/~offutt/classes/642/examples/servlets/ 6/5/2019 © Offutt

Logging Error Data Why send error data to the SysAdmin? If users make the same mistake repeatedly, the web page and instructions should be re-designed 6/5/2019 © Offutt

Logging Error Data (2) Use HttpServletResponse object Two methods can be used : sendError (int statusCode) sendError (int statusCode, String message) The sendError() methods do two things: Sets the response status code “Commits” the response: Puts message in an error log Usually sends an HTML page to the user Message should be user oriented! 6/5/2019 © Offutt

Logging Error Data (3) HTTP status codes : 400 : Syntactically incorrect 401 : Requires HTTP authentication 403 : The server refused to fulfill request 404 : Resource is unavailable 500 : An error in the HTTP server 501 : Server does not support the functionality needed 503 : HTTP server is overloaded Using the builtin methods saves trouble and can be more reliable, and saves information into the log 6/5/2019 © Offutt

Logging Error Data (4) You can also put messages into the log without bothering the user: Get a ServletContext object from the ServletConfig object: ServletContext sc = getServletContext(); Call the log() method: sc.log (“JOServlet: method m1() was executed.”); This message is sent to a log file on the system: the location of the log file is determined when the web server is installed localhost : C:\Tomcat\logs\ apps-swe642 : /var/tomcat/logs/catalina.out 6/5/2019 © Offutt

Servlet Exceptions “do-do” methods have “throws ServletException” If your servlet throws a ServletException, serious things happen: Execution stops Response object is not returned to user Servlet engine takes control and usually : Client gets a cryptic error message (sending the user to another web site!) The error is logged This is a last resort technique! 6/5/2019 © Offutt

Summary—User Responses to Exceptions The previous discusses how to handle exceptions, but the designer must think carefully about what to do to handle exceptions. This is properly a subject of SWE 632, User Interface Design … 6/5/2019 © Offutt