Dealing with Runtime Errors

Slides:



Advertisements
Similar presentations
Topics Introduction Types of Errors Exceptions Exception Handling
Advertisements

Lecture 9. 2 Exception  An exception is a unusual, often unpredictable event, detectable by software or hardware, that requires special processing occurring.
Introduction to Computing Using Python Exceptions (Oops! When things go wrong)  Errors and Exceptions  Syntax errors  State (execution) errors.
Lecture 07 – Exceptions.  Understand the flow of control that occurs with exceptions  try, except, finally  Use exceptions to handle unexpected runtime.
Lecture 27 Exceptions COMP1681 / SE15 Introduction to Programming.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
 2002 Prentice Hall. All rights reserved Exception-Handling Overview Exception handling –improves program clarity and modifiability by removing.
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.
Exceptions COMPSCI 105 S Principles of Computer Science.
1 Web Based Programming Section 6 James King 12 August 2003.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 19 Handling Exceptions 6/09/09 Python Mini-Course: Lesson 19 1.
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
WEEK EXCEPTION HANDLING. Syntax Errors Syntax errors, also known as parsing errors, are perhaps the most common kind of complaint you get while.
November 15, 2005ICP: Chapter 7: Files and Exceptions 1 Introduction to Computer Programming Chapter 7: Files and Exceptions Michael Scherger Department.
General Programming Introduction to Computing Science and Programming I.
Exceptions 2 COMPSCI 105 S Principles of Computer Science.
Guide to Programming with Python Chapter Seven (Part 1) Files and Exceptions: The Trivia Challenge Game.
Exceptions COMPSCI 105 SS 2015 Principles of Computer Science.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 7 Files.
Cem Sahin CS  There are two distinguishable kinds of errors: Python's Errors Syntax ErrorsExceptions.
1. Understand how to catch errors in a program 2. Know how to filter user input to stop incorrect data.
Guide to Programming with Python Chapter Seven Files and Exceptions: The Trivia Challenge Game.
Exceptions CMSC 201. Overview Exceptions are run-time errors, especially ones that the programmer cannot predict.  example 1: division by zero  example.
11. EXCEPTION HANDLING Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Firoze Abdur Rakib. Syntax errors, also known as parsing errors, are perhaps the most common kind of error you encounter while you are still learning.
CS 127 Exceptions and Decision Structures. Exception Handling This concept was created to allow a programmer to write code that catches and deals with.
Introduction to Computing Using Python Exceptions (Oops! When things go wrong)  Errors and Exceptions  Syntax errors  State (execution) errors.
Lecture 07 – Exceptions.  Understand the flow of control that occurs with exceptions  try, except, finally  Use exceptions to handle unexpected runtime.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Exception Handling How to handle the runtime errors.
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. Catching exceptions Whenever a runtime error occurs, it create an exception object. The program stops running at this point and Python prints.
Python: Exception Handling Damian Gordon. Exception Handling When an error occurs in a program that causes the program to crash, we call that an “exception”
16 Exception Handling.
Exceptions in Python Error Handling.
Introduction to Computing Science and Programming I
CMPT 120 Topic: Python’s building blocks -> More Statements
George Mason University
Why exception handling in C++?
Part IX Fundamentals of C and C++ Programming Exception Handling
Handling errors try except
Exceptions and files Taken from notes by Dr. Neil Moore
Exceptions 10-Nov-18.
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
Exceptions with Functions
Validations and Error Handling
Chapter 17 Templates and Exceptions Part 2
Python’s Errors and Exceptions
Exceptions and files Taken from notes by Dr. Neil Moore
ERRORS AND EXCEPTIONS Errors:
(Oops! When things go wrong)
Exceptions.
Input from the Console This video shows how to do CONSOLE input.
Managing Errors and Exceptions
Problems Debugging is fine and dandy, but remember we divided problems into compile-time problems and runtime problems? Debugging only copes with the former.
Python Syntax Errors and Exceptions
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
By Ryan Christen Errors and Exceptions.
Introduction to Programming
Exceptions 10-May-19.
Debugging and Handling Exceptions
Exception Handling COSC 1323.
Topics Introduction to File Input and Output
Lecture 9.
Chapter 11: Exception Handling
Python Exceptions and bug handling
Data Types and Expressions
Flow Control I Branching and Looping.
Debugging 101 Exterminating Vermin.
Presentation transcript:

Dealing with Runtime Errors Exception Handling Dealing with Runtime Errors

Exception An exception is an error that occurs at runtime Types of exceptions: NameError Caused by: Misspelled variable or function name Use of variable in expression before it is assigned an initial value TypeError Operand of incorrect type for operator ValueError Passing wrong type of data to function

Exception Handling You can write an exception handler to intercept an exception and prevent your program from crashing. Use a try - except statement to intercept exceptions: try: # guarded statements except Exception as e: # deal with the exception

Exception Handling Example How try-except works: Guarded statements execute one at a time If an exception occurs while executing a statement, the rest of the guarded statements are skipped, and the exception handler executes After a try-except statement executes, the program continues normally try: ageStr = input('Enter your age:') age = int(ageStr) print('You are', age, 'years old.') except Exception as e: print('Something went wrong: ', e)

Exception Parameter You can optionally define an exception parameter with “as”: except Exception as e The exception parameter contains information about the exception Print out the exception parameter so that the user knows what went wrong try: ageStr = input('Enter your age:') age = int(ageStr) print('You are', age, 'years old.') except Exception as e: print('Something went wrong: ', e)

Exception-Specific Handling Different kinds of exceptions occur ValueError - attempt to convert non- numeric data to int or float ZeroDivisionError - division by zero A try-except statement can perform exception-specific handling by defining more than one exception handler Put the general Exception as e case at the end Exception handlers are tried in order The first handler that matches the exception will handle it try: total = float(input('Enter total amount:')) count = float(input('Enter count:')) avg = total / count print('average =', avg) except ValueError: print('Invalid input') except ZeroDivisionError: print('Can't divide by zero') except Exception as e: print('Something went wrong: ', e)

Exception Propagation An exception that occurs inside a function propagates to its caller if not handled A single exception handler can catch exceptions that occur anywhere in the program However, all it can do is a graceful shutdown Pro tip: Avoid defining a “master” exception handler like this during program development Complicates debugging def square(n): return int(n) * int(n) def main(): num = input('Enter a number:') print(num, 'squared is:', square(num)) try: main() except Exception as e: print('Something went wrong: ', e)

Exception Handling in Web Apps Need to display a clean error message to user Return an HTML page with a user-friendly error message Need to log a detailed error message to console Use traceback module: import traceback traceback.print_exc() Example: examples/webapps/webguess.py