EXCEPTIONS. Catching exceptions Whenever a runtime error occurs, it create an exception object. The program stops running at this point and Python prints.

Slides:



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

Exceptions CSE301 University of Sunderland Harry Erwin, PhD.
Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
Exceptions: when things go wrong. Various sources of error public static doSomething() { int i = 3.0; while(!done); { int i = false } ) Syntactic errors.
CSM-Java Programming-I Spring,2005 Exceptions Lesson - 7.
An Introduction to Java Programming and Object- Oriented Application Development Chapter 8 Exceptions and Assertions.
Index Exception handling Exception In Java Exception Types
EXCEPTIONS. What’s an exception?? Change the flow of control when something important happens ideally - we catch errors at compile time doesn’t happen.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Understand Error Handling Software Development Fundamentals LESSON 1.4.
Lecture 07 – Exceptions.  Understand the flow of control that occurs with exceptions  try, except, finally  Use exceptions to handle unexpected runtime.
CSI 3120, Exception handling, page 1 Exception and Event Handling Credits Robert W. Sebesta, Concepts of Programming Languages, 8 th ed., 2007 Dr. Nathalie.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
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.
Handling Errors Introduction to Computing Science and Programming I.
 2002 Prentice Hall. All rights reserved Exception-Handling Overview Exception handling –improves program clarity and modifiability by removing.
Exceptions COMPSCI 105 S Principles of Computer Science.
Chapter 13 Exception Handling F Claiming Exceptions F Throwing Exceptions F Catching Exceptions F Rethrowing Exceptions  The finally Clause F Cautions.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 19 Handling Exceptions 6/09/09 Python Mini-Course: Lesson 19 1.
Object Oriented Programming
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.
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.
Slides Credit Umair Javed LUMS Web Application Development.
17. Python Exceptions Handling Python provides two very important features to handle any unexpected error in your Python programs and to add debugging.
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.
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?
Dealing with Errors. Error Types Syntax Errors Runtime Errors Logical Errors.
VB.Net - Exceptions Copyright © Martin Schray
Chapter 24 Exception CSC1310 Fall Exceptions Exceptions Exceptions are events that can modify the flow or control through a program. They are automatically.
Exception Handling Unit-6. Introduction An exception is a problem that arises during the execution of a program. An exception can occur for many different.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 7 Files.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
Cem Sahin CS  There are two distinguishable kinds of errors: Python's Errors Syntax ErrorsExceptions.
© Mohamed Nuzrath Java Programming :: Chapter 6 :: Prepared & Presented By :: Mohamed Nuzrath [ Major In Programming ] NCC Programme coordinator IT Lecturer.
Guide to Programming with Python Chapter Seven Files and Exceptions: The Trivia Challenge Game.
CPS120 Introduction to Computer Science Iteration (Looping)
Exceptions CMSC 201. Overview Exceptions are run-time errors, especially ones that the programmer cannot predict.  example 1: division by zero  example.
Exceptions and Assertions Chapter 15 – CSCI 1302.
11. EXCEPTION HANDLING Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Exception-Handling Fundamentals  A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Error Handling Tonga Institute of Higher Education.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
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.
Lecture 4 Python Basics Part 3.
JavaScript and Ajax (Control Structures) Week 4 Web site:
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
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.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
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.
Introduction to Exceptions in Java CS201, SW Development Methods.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Lecture 11 Dr. Eng. Ibrahim El-Nahry Exception Handling.
Exceptions in Python Error Handling.
Exceptions and files Taken from notes by Dr. Neil Moore
EE422C Software Implementation II
Python Primer 2: Functions and Control Flow
Topics Introduction to File Input and Output
Exception Handling.
Python’s Errors and Exceptions
Exceptions and files Taken from notes by Dr. Neil Moore
ERRORS AND EXCEPTIONS Errors:
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Exceptions 10-May-19.
Topics Introduction to File Input and Output
Dealing with Runtime Errors
Presentation transcript:

EXCEPTIONS

Catching exceptions Whenever a runtime error occurs, it create an exception object. The program stops running at this point and Python prints out the traceback, which ends with a message describing the exception that occurred.

Dividing by zero creates an exception

Accessing a non-existent list item

Trying to make an item assignment on a tuple

Error messages In each case, the error message on the last line has two parts: -The type of error before the colon. -Specifics about the error after the colon.

try statement Sometimes we want to execute an operation that might cause an exception, but we don’t want the program to stop. We can handle the exception using try statement to “wrap” a region of code,

Handle the exception

try statement The try statement has three separate clauses, introduced by the keywords try … except … finally The try statement executes and monitors the statements in the first block. If no exception occurs. It executes the statements in the except clause and then continues.

We could encapsulate this capability in a function: exists which takes a filename and return true if the file exists, false if it doesn’t:

Raising our own exceptions

raise my_error creates an exception object, ValueError object, which encapsulates specific information about the error.

Assume function A called B which called C which called D which called get_age. The raise statement carries this object out as a kind of “return value”, and immediate it exits from get_age() to its caller D. Then D again exits to its caller C, And C exits to B and so on, each returning the exception object to their caller, until it encounters a try … except that can handle the exception.

ValueError ValueError is one of the built-in exception types which most closely matches the kind of error we want to raise

If the function that called get_age (or its caller) handles the error, then the program can carry on running: otherwise, Python prints the traceback and exits:

The finally clause