1 Software Construction and Evolution - CSSE 375 Exception Handling - Principles Steve Chenoweth, RHIT Above – Exception handling on the ENIAC. From

Slides:



Advertisements
Similar presentations
COMP 121 Week 5: Exceptions and Exception Handling.
Advertisements

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 13 Exception Handling.
Objectives In this chapter you will: Learn what an exception is Learn how to handle exceptions within a program See how a try / catch block is used to.
Exceptions Any number of exceptional circumstances may arise during program execution that cause trouble import java.io.*; class IOExample { public static.
Error Handling with Exceptions Concepts C and other earlier languages often had multiple error-handling schemes, and these were generally established.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
Lecture 27 Exceptions COMP1681 / SE15 Introduction to Programming.
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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
Exceptions Used to signal errors or unexpected situations to calling code Should not be used for problems that can be dealt with reasonably within local.
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 CIS 304 Intermediate Java Programming for Business.
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.
1 Lecture#8: EXCEPTION HANDLING Overview l What exceptions should be handled or thrown ? l The syntax of the try statement. l The semantics of the try.
12.1 Exceptions The limitations of traditional methods of exception handling Error conditions are a certainty in programming Programmers make.
Introduction to Java Chapter 11 Error Handling. Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle.
June 14, 2001Exception Handling in Java1 Richard S. Huntrods June 14, 2001 University of Calgary.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
Object Oriented Programming
CIS 270—Application Development II Chapter 13—Exception Handling.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Slides Credit Umair Javed LUMS Web Application Development.
Exceptions 1. Your computer takes exception Exceptions are errors in the logic of a program (run-time errors). Examples: Exception in thread “main” java.io.FileNotFoundException:
Exception Handling Basic -- from generic idea to Java.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
1 Software Construction and Evolution - CSSE 375 Exception Handling – Logging & Special Situations Steve Chenoweth Office: Moench Room F220 Phone: (812)
Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…
Exceptions Handling Exceptionally Sticky Problems.
Object Oriented Software Development 8. Exceptions, testing and debugging.
Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters
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.
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
Odds and Ends. CS 21a 09/18/05 L14: Odds & Ends Slide 2 Copyright © 2005, by the authors of these slides, and Ateneo de Manila University. All rights.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
1 Software Construction and Evolution - CSSE 375 Exception Handling – Chaining & Threading Steve Chenoweth Office: Moench Room F220 Phone: (812)
Exceptions and Assertions Chapter 15 – CSCI 1302.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
Exceptions. Exception  Abnormal event occurring during program execution  Examples Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
Creating a GUI Class An example of class design using inheritance and interfaces.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 14 Event-Driven Programming with Graphical User Interfaces.
Java Programming: Exceptions1 Exceptions Reference: java.sun.com/docs/books/tutorial/essential/exceptions/
(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.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Exceptions in OO Programming Introduction Errors Exceptions in Java Handling exceptions The Try-Catch-Finally mechanism Example code Exception propagation.
Exceptions an unusual condition – e.g. division by zero – e.g. file doesn't exist – e.g. illegal type – etc. etc… typically a run-time error – i.e. during.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Introduction to Exceptions in Java CS201, SW Development Methods.
Java Exceptions a quick review….
Handling Exceptionally Sticky Problems
Exceptions 10-Nov-18.
Exceptions 10-Nov-18.
Handling Exceptions.
Exceptions Handling the unexpected
Web Design & Development Lecture 7
Exceptions 25-Apr-19.
Exceptions 22-Apr-19.
CSC 143 Java Errors and Exceptions.
Handling Exceptionally Sticky Problems
Exception Handling Contents
Exceptions 10-May-19.
Debugging and Handling Exceptions
Exceptions 5-Jul-19.
CMSC 202 Exceptions.
Presentation transcript:

1 Software Construction and Evolution - CSSE 375 Exception Handling - Principles Steve Chenoweth, RHIT Above – Exception handling on the ENIAC. From ating_ENIAC.gif. ating_ENIAC.gif Left – Our whole software field is about generating novelty. Once you discover everyone follows some rule, it’s almost immediately time to discover what happens if you break that. Here, clothes are supposed to be symmetrical, left vs right. So, how about if they are not?

2 What is it? A problem The detection of that problem A message showing we detected the problem Ways to resolve, log, or otherwise act on the problem Ways to make your system more robust Ways to make it more like a product than a project Above – Our software exception handling methods mimic sequences we can picture occurring, often taken from situations where humans would be performing those steps. From websphere/library/techarticles/wasid/ WPC_Concepts/WPC_Concepts.html. websphere/library/techarticles/wasid/ WPC_Concepts/WPC_Concepts.html Q1

3 A perfect example - GUIs Users do things that cause problems For some, there’s an action of – Notifying the user – Asking them to fix the problem – Rechecking to see if they did Users expect some level of help like this Clients - ditto Q2 IndividualHW1, due next class Thursday, is all about controlling GUI’s!

4 Another example – input from other systems In pipes-and-filters systems, many steps are essentially checking for errors Q3

5 Languages have support for this System.out.print("Enter your name: "); // open up standard input BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String userName = null; // read the username from the command-line; need to use try/catch with the // readLine() method try { userName = br.readLine(); } catch (IOException ioe) { System.out.println("IO error trying to read your name!"); System.exit(1); } System.out.println("Thanks for the name, " + userName);

6 You need a design for each exception! E.g. – public void connect(String server, int port) throws java.io.IOException What error sequence is appropriate for the calling class to do? Q4

7 Standard options are used! Log the exception or relevant info Ask the user or application for input Use default values or alternate data Forward control to some other part of the app Convert the exception to another form Ignore the problem Retry the action Take an alternate or recovery action Prepare the system for shutdown A preview of “Defensive programming” coming up!

8 Asking the user for input Assumes you have a “user” who can do this! Typical sequence – – Detect the error – In the error routine – – Give the user choices or let them type a choice – Retry the action – Have a “way out” if they give up Variation – give the user a chance to avoid a likely error! Q5

9 Forwarding control Pretty commonly done – like “throwing” exceptions in Java, to the calling class Exceptions often are detected at a lower level, but only the higher-level app really knows what to do – Like in the connection example, on Slide 6 – We’ll see the ins and outs of this, shortly…

10 Exception handling rules “Catch it if you can” Handle specifically, but systematically Log anything that makes sense to log Convert to a “business context” Q6

11 And… Systematically analyze possible errors – Analysis should include “errors that we could avoid in the first place” Rate the risk of not dealing with these, vs the cost of handling them Write down your analysis! Don’t ignore them and let the run time blow up as a result, etc. Don’t assume “they’ll never happen” Q7

12 What’s the buggiest code? It’s the error handling code! – Why? – Because it’s tested less than other code. How to improve on that situation: – Write complete error-handling test cases. – Trace what’s tested in the code, using a coverage tool. Target backup and recovery code for the most testing! Q8

13 The article you read What’s good about try / catch? – Keeps exceptions out of the main line of logic. – Makes what’s “supposed to happen” easier to read. What’s good about “throwing” exceptions? – Sends them up to caller, if appropriate. – Caller may better know what to do! – May be able to consolidate actions there, providing consistency: Like what to do if a file doesn’t open. What to do if a user enters a field incorrectly. How to handle missing DB data, in this case, using a common DB handler

14 But… What’s bad about try/catch? – The error code goes off to a different place. – What if it needs to rejoin the main line of code, say, to repeat what caused the error? What’s bad about “throwing” exceptions? – Programmers “throw” exceptions just because they don’t want to deal with them, and – The calling routine has no idea what the errors mean! – And they handle them in all kinds of incongruous ways. Q9 – 11

15 Program you looked at… Console UI Keep trying till you get what you want! User overrides the usual inputs.

16 How the user overrides are created… Note one line of input here, many lines of error checking! We created our own type of exception, which was “thrown” to main.

17 Here’s how we made the exception… We could have “featured-up” this class!