Handling Errors Introduction to Computing Science and Programming I.

Slides:



Advertisements
Similar presentations
Pearson Education, Inc. All rights reserved. 1.. Exception Handling.
Advertisements

CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
COMPUTER PROGRAMMING I Essential Standard 5.02 Understand Breakpoint, Watch Window, and Try And Catch to Find Errors.
An Introduction to Java Programming and Object- Oriented Application Development Chapter 8 Exceptions and Assertions.
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.
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.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
CS0004: Introduction to Programming Repetition – Do Loops.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Python Magic Select a Lesson: Why Learn to Code? Basic Python Syntax
Lecture 27 Exceptions COMP1681 / SE15 Introduction to Programming.
Debugging Introduction to Computing Science and Programming I.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Chapter 61 Flags A flag is a variable that keeps track of whether a certain situation has occurred. The data type most suited to flags is Boolean.
New Mexico Computer Science For All More Looping in NetLogo Maureen Psaila-Dombrowski.
Introduction to Computer Programming Error Handling.
Exceptions COMPSCI 105 S Principles of Computer Science.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about.
CIS 270—Application Development II Chapter 13—Exception Handling.
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.
COMP 1001: Introduction to Computers for Arts and Social Sciences Programming in Scratch Monday, May 16, 2011.
General Programming Introduction to Computing Science and Programming I.
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?
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
CPS120 Introduction to Computer Science Iteration (Looping)
Exception Handling in JAVA. Introduction Exception is an abnormal condition that arises when executing a program. In the languages that do not support.
Visual Basic.NET BASICS Lesson 5 Exponentiation, Order of Operations, and Error Handling.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
FT228/3 Web Development Error processing. Introduction READ Chapter 9 of Java Server Pages from O’reilly 2 nd Edition Need to be able to 1) Diagnose and.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Exceptions Chapter 16 This chapter explains: What as exception is Why they are useful Java exception facilities.
CPS120 Introduction to Computer Science Iteration (Looping)
Review while loops Control variables Example Infinite loop
Event Handling Tonga Institute of Higher Education.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
11. EXCEPTION HANDLING Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
Error Handling Tonga Institute of Higher Education.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Intro to Loops 1.General Knowledge 2.Two Types of Loops 3.The WHILE loop 1.
Exception Handling and String Manipulation. Exceptions An exception is an error that causes a program to halt while it’s running In other words, it something.
IMS 3253: Validation and Errors 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Validation and Error Handling Validation.
AVCE ICT – Unit 7 - Programming Session 12 - Debugging.
Exception Handling How to handle the runtime errors.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students, continue; and break; statements.
5a – While Loops Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
Control Structures WHILE Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied.
Exception Handling. VB.NET has an inbuilt class that deals with errors. The Class is called Exception. When an exception error is found, an Exception.
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.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Exception testing Aistis Karkauskas, IFM-2/2. Introduction  Exceptions – anomalous or exceptional events requiring special processing – often changing.
Chapter 14: Exception Handling
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Selection CIS 40 – Introduction to Programming in Python
Topics Introduction to File Input and Output
Exception Handling.
Higher Computing Using Loops.
CMPT 102 Introduction to Scientific Computer Programming
By Hector M Lugo-Cordero September 3, 2008
A LESSON IN LOOPING What is a loop?
Introduction to Computer Science
GCSE Computing:: While Loops
Topics Introduction to File Input and Output
How to allow the program to know when to stop a loop.
Presentation transcript:

Handling Errors Introduction to Computing Science and Programming I

Exceptions When an error occurs while a program is running it is called an exception. When an error occurs while a program is running it is called an exception. If you know that an exception may occur in your program you can use the try – except structure to catch the error and prevent your program from crashing. If you know that an exception may occur in your program you can use the try – except structure to catch the error and prevent your program from crashing.

try – except structure try:codeblockexcept:codeblock If an error occurs while in the try codeblock, execution in that codeblock will immediately stop and the code in the except codeblock will be executed. If no error occurs, the code in the except block will be ignored. If an error occurs while in the try codeblock, execution in that codeblock will immediately stop and the code in the except codeblock will be executed. If no error occurs, the code in the except block will be ignored.

try – except structure m_str = raw_input("Enter your height (in metres): ") try: metres = float(m_str) feet = * metres / 12 print "You are " + str(feet) + " feet tall." except: print "That wasn’t a number.“ If the user doesn’t enter a number they will see an error message. You can then add a while loop to keep prompting the user until they enter a number. If the user doesn’t enter a number they will see an error message. You can then add a while loop to keep prompting the user until they enter a number.

try – except structure got_height = False while not got_height: m_str = raw_input("Enter your height (in metres): ") try: metres = float(m_str) feet = * metres / 12 print "You are " + str(feet) + " feet tall.“ got_height = True except: print "That wasn’t a number.“ When there’s an error in the float conversion execution will exit the try block before setting got_height to True. When there’s an error in the float conversion execution will exit the try block before setting got_height to True.