Exceptions & Error Handling

Slides:



Advertisements
Similar presentations
Exception Handling. Background In a perfect world, users would never enter data in the wrong form, files they choose to open would always exist, and code.
Advertisements

Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
EXCEPTIONS. What’s an exception?? Change the flow of control when something important happens ideally - we catch errors at compile time doesn’t happen.
Exceptions Briana B. Morrison CSE 1302C Spring 2010.
CPSC150 Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 1 CPSC150 Exceptions When things.
11-Jun-15 Exceptions. 2 Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
16-Jun-15 Exceptions. Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a null.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
Java Review 2 – Errors, Exceptions, Debugging Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
Java Software Solutions Foundations of Program Design Sixth Edition
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
June 14, 2001Exception Handling in Java1 Richard S. Huntrods June 14, 2001 University of Calgary.
Exception Handling 1. Introduction Users may use our programs in an unexpected ways. Due to design errors or coding errors, our programs may fail in unexpected.
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
CIS 270—Application Development II Chapter 13—Exception Handling.
COMPUTER PROGRAMMING 2 Exceptions. What are Exceptions? Unexpected events that happen when the code is executing (during runtime). Exceptions are types.
Slides Credit Umair Javed LUMS Web Application Development.
Errors And How to Handle Them. GIGO There is a saying in computer science: “Garbage in, garbage out.” Is this true, or is it just an excuse for bad programming?
Exceptions Syntax, semantics, and pragmatics Exceptions1.
COMP Exception Handling Yi Hong June 10, 2015.
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
Exceptions Lecture 11 COMP 401, Fall /25/2014.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Introduction to Exceptions in Java CS201, SW Development Methods.
Exceptions in the Java programming language J. W. Rider.
Lec.11 (Chapter 11) Exception Jiang (Jen) ZHENG July 13 th, 2005.
Eighth Lecture Exception Handling in Java
C++ Exceptions.
Chapter 10 – Exception Handling
Tirgul 13 Exceptions 1.
MIT AITI 2003 Lecture14 Exceptions
Syntax, semantics, and pragmatics
Exceptions C++ Interlude 3
Chapter 14: Exception Handling
Exceptions 10-Nov-18.
Exceptions 10-Nov-18.
CNS 3260 C# .NET Software Development
Exception Handling Chapter 9.
Advanced Java Programming
Chapter 12 Exception Handling
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 and Reading / Writing Files
Exception Handling Chapter 9 Edited by JJ.
Part B – Structured Exception Handling
Web Design & Development Lecture 7
Exception Handling Imran Rashid CTO at ManiWeber Technologies.
Exceptions 19-Feb-19.
Java Exceptions Dan Fleck CS211.
Exceptions 7-Apr-19.
Exceptions 25-Apr-19.
Errors and Exceptions Error Errors are the wrongs that can make a program to go wrong. An error may produce an incorrect output or may terminate the execution.
Exceptions 22-Apr-19.
Chapter 12 Exception Handling and Text IO Part 1
CSC 143 Java Errors and Exceptions.
Exceptions 10-May-19.
Exceptions References: Jacquie Barker, Beginning Java Objects; Rick Mercer, Computing Fundamentals With Java; Wirfs-Brock et. al., Martin Fowler, OOPSLA.
Java Basics Exception Handling.
Exceptions 5-Jul-19.
CMSC 202 Exceptions 2nd Lecture.
CMSC 202 Exceptions.
Exception Handling.
Exceptions and Exception Handling
Presentation transcript:

Exceptions & Error Handling Christopher M. Pascucci 24-Nov-18

Errors and Exceptions An error is a bug in your program. dividing by zero going outside the bounds of an array trying to use a null object or reference An exception is a problem whose cause is outside your program. trying to open a file that isn’t there running out of memory connecting or working with a database and SQL

What to do about errors and exceptions An error is a bug in your program that occurs during execution of program code. It should be fixed. An exception is a problem that your program may encounter. The source of the problem is outside your program. Your program must be prepared to deal with it. The distinction between the two is that an error is an event and an exception is an object that was created by the event that contains where and when the error occurred.

Dealing with exceptions Most exceptions arise when you are handling files or working with databases. A needed file may be missing. You may not have permission to write a database or file. A file or field may be the wrong type. Exceptions may also arise when you use someone else’s classes (or they use yours). You might use a class incorrectly. Incorrect usage should result in an exception.

Three approaches to error checking Ignore all but the most important errors. The code is cleaner, but the program will misbehave when it encounters an unusual error. Do something appropriate for every error. The code is cluttered, but the program works better. You might still forget some error conditions. Do the normal processing in one place, handle the errors in another (this is the .NET way). The code is at least reasonably uncluttered. .NET tries to ensure that you handle every error.

The try statement .NET provides a control structure, the Try statement (also called the Try-Catch statement) to separate “normal” code from error handling: Try try this code and if an error occurs during this code it will be handled in the catch. Catch some exception handle the exception Catch some other exception (***optional) handle the exception Finally Optional code block that always executes after the try-catch Whether or not an exception occurred. End Try

Exception handling is not optional As in other languages, errors usually just cause your program to crash. Other languages leave it up to you whether you want to handle exceptions. There are a lot of sloppy programs in the world. It’s normal for human beings to be lazy. .NET tries to force you to handle exceptions. This is sometimes a pain in the neck, but... the result is almost always a better program.

Error and Exception are Objects When an error occurs, .NET throws an Exception and creates an Exception object for you to use. You can catch this object and try to recover. You can ignore the error (the program will crash). .NET also provides a global Err object that can be used like in previous versions of VB. Err.number – displays the error number. Err.Description – displays the error message.

A few common types of Exceptions IO.IOException: a problem doing input/output. IO.FileNotFoundException: no such file. IO.EndOfStreamException: tried to read past the end of an i/o stream. NullReferenceException: tried to use a object that was actually null (this is a RuntimeException). InvalidCastException: tried to convert a non-numeric String to a number or some other invalid casting. IndexOutofRangeException: tried to access an array element using an index that is outside the bounds of an array. Data.DataException: error generated by using ADO.NET components. Data.SqlClient.SqlException: error generated by an SQL server. ArgumentException: generated when at least one of the arguments past to a method is not valid.

What to do about Exceptions You have three choices: You can deal with it at the Page level using Try-Catch through out the page using the Page_Error event of the page You can deal with it at the Application level using the Application_Error procedure in the global.asax file. You can deal with it in the customError section of the web.config file. <customErrors mode="On"      defaultRedirect="myerror.htm">     <error statusCode="403"         redirect="authfailed.aspx"/>     <error statusCode="404"         redirect="filenotfound.aspx"/> </customErrors> 

Try-Catch-Finally After all the catch phrases, you can have an optional Finally phrase. Try Catch e as ExceptionType Catch e as AnotherExceptionType Finally End Try Whatever happens in Try and Catch, even if it does a Return statement, the Finally code will be executed. If no exception occurs, the Finally will be executed after the Try code. If an exception does occur, the Finally will be executed after the appropriate Catch code.

How the Try statement works The code in the Try block part is executed. If there are no problems, the Catch phrases are skipped. If an exception occurs, the program jumps immediately to the first Catch clause that can handle that exception. Whether or not an exception occurred, the Finally code is executed.

Ordering the Catch phrases A Try can be followed by many Catches. The first one that can catch the exception is the one that will catch the exception. Bad: Catch e As Exception Catch e As IO.IOException This is bad because IOException is a subclass of Exception, so any IOException will be handled by the first Catch. The second Catch phrase can never be used. Start by ordering Catches from the more specific down to the more general.

Constructing an Exception Exceptions are classes; you can create your own Exception with new. Exception types have two constructors: one with no parameters, and one with a String parameter. But first, you should look through the predefined exceptions to see if there is already one that’s appropriate. Public Class NotEnoughPizzaException     Inherits System.ApplicationException     Public Sub New()     End Sub     Public Sub New(ByVal strMessage As String)         MyBase.New(strMessage)     End Sub End Class Try   Throw New NotEnoughPizzaException(_        "Better order more") Catch ex As NotEnoughPizzaException   Response.Write(ex.Message) End Try

The End… Additional Resources: Exception Hierarchy Comprehensive List of Exceptions & Hierarchy