Dealing with Errors. Error Types Syntax Errors Runtime Errors Logical Errors.

Slides:



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

An Introduction to Java Programming and Object- Oriented Application Development Chapter 8 Exceptions and Assertions.
© The McGraw-Hill Companies, 2006 Chapter 15. © The McGraw-Hill Companies, 2006 Exceptions an exception is an event that occurs during the life of a program.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.
Programming Types of Testing.
BIM313 – Advanced Programming Techniques Debugging 1.
Exceptions CIS 304 Intermediate Java Programming for Business.
How to Debug VB .NET Code.
Computer Science 1620 Programming & Problem Solving.
 2002 Prentice Hall. All rights reserved Exception-Handling Overview Exception handling –improves program clarity and modifiability by removing.
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
Activity 1 - WBs 5 mins Go online and spend a moment trying to find out the difference between: HIGH LEVEL programming languages and LOW LEVEL programming.
Exceptions COMPSCI 105 S Principles of Computer Science.
The University of Texas – Pan American
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.
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.
Program Development Life Cycle (PDLC)
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
Cem Sahin CS  There are two distinguishable kinds of errors: Python's Errors Syntax ErrorsExceptions.
Review, Pseudocode, Flow Charting, and Storyboarding.
Programming, an introduction to Pascal
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.
1 Program Planning and Design Important stages before actual program is written.
School of Computer Science & Information Technology G6DICP - Lecture 6 Errors, bugs and debugging.
Exceptions and Assertions Chapter 15 – CSCI 1302.
11. EXCEPTION HANDLING Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
CS 177 Week 10 Recitation Slides 1 1 Debugging. Announcements 2 2.
5.01 Understand Different Types of Programming Errors
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
CS 127 Exceptions and Decision Structures. Exception Handling This concept was created to allow a programmer to write code that catches and deals with.
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.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Head First Python: Ch 3. Files and Exceptions: Dealing with Errors Aug 26, 2013 Kyung-Bin Lim.
COMPUTER PROGRAMMING I SUMMER Understand Different Types of Programming Errors.
Exception and Exception Handling. Exception An abnormal event that is likely to happen during program is execution Computer could run out of memory Calling.
EXCEPTIONS. Catching exceptions Whenever a runtime error occurs, it create an exception object. The program stops running at this point and Python prints.
Lecture 3 Computer Programming -1-. The main steps of program development to solve the problem: 1- problem definition : The problem must be defined into.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Exceptions in Python Error Handling.
Introduction to Computing Science and Programming I
5.01 Understand Different Types of Programming Errors
Data Types Variables are used in programs to store items of data e.g a name, a high score, an exam mark. The data stored in a variable is entered from.
Introduction to Python
Can you pick 3 errors from this
Handling errors try except
Exceptions and files Taken from notes by Dr. Neil Moore
Unit# 8: Introduction to Computer Programming
5.01 Understand Different Types of Programming Errors
Selection CIS 40 – Introduction to Programming in Python
Validations and Error Handling
Exceptions Problems in a Java program may cause exceptions or errors representing unusual or invalid processing. An exception is an object that defines.
Exceptions and files Taken from notes by Dr. Neil Moore
Chapter 3 – Control Structures
ERRORS AND EXCEPTIONS Errors:
Exceptions.
Software Development Process
Problems Debugging is fine and dandy, but remember we divided problems into compile-time problems and runtime problems? Debugging only copes with the former.
CHAPTER 6: Control Flow Tools (for and while loops)
Learning Intention I will learn about the different types of programming errors.
Debugging and Handling Exceptions
1.3.7 High- and low-level languages and their translators
CHAPTER 6 Testing and Debugging.
Presentation transcript:

Dealing with Errors

Error Types Syntax Errors Runtime Errors Logical Errors

Syntax Error Where the programmer enters something that breaks the rule of the language… or…uses a token (letter, symbol or operator) that is not defined in the grammar of the language. >>> if a > b >>> print(‘A is greater than B!’) A syntax error has occurred! >>> if a > b : >>> print(‘A is greater than B!’)

Syntax Error num1 = int(input('What is your first number? ')) num2 = int(input('What is your second number? ')) num3 = int(input('What is your third number? ')) num4 = int(input('What is your fourth number? ')) num5 = int(input('What is your fifth number? ')) sum = num1+num2+num3+num4+num5 Print('The sum is: ' sum) Average:(sum/5) pint('The average is: ', average)) Syntax errors are quite easy to spot and fix. The compiler debugger (and an interpreter) will raise this error when it notices that a syntax error has occurred. How many syntax errors can you spot below?

Syntax Error How many syntax errors can you spot below? 5 Syntax Errors! num1 = int(input('What is your first number? ')) num2 = int(input('What is your second number? ')) num3 = int(input('What is your third number? ')) num4 = int(input('What is your fourth number? ')) num5 = int(input('What is your fifth number? ')) sum = num1+num2+num3+num4+num5 print('The sum is: ', sum) average=(sum/5) print('The average is: ', average)

Runtime Error These errors occur whenever the program instructs the computer to carry out an operation that it is not designed to do or very slow to do. One of the most common runtime errors is when a program instructs a computer to divide any number by the value zero >>> print(1/0) This operation produces an infinitely large result, which is too large for a computer to accommodate. Here is another runtime error – adding a string and a number. >>> print("you cannot add text and numbers" + 12)

Syntax or Runtime Error? print("Here is some text") print(1/0)

Syntax or Runtime Error? print("Here is some text") print(1*0 Remember, a program with a syntax error will execute NO steps at all!

Logical Error These types of errors are regarded as the most difficult to spot. Similar to the runtime error, the interpreter will not give you any message or warning and the program will run perfectly fine without hanging. However, the program will not behave in the manner it was designed to. In other words, it will simply produce incorrect results. if score > 50: print(‘C’) elif score >75: print(‘B’) elif score >85: print(‘A’) Anna scores 80 in her test, however, when she puts her score into the program, it displays a ‘C’ grade…and not the expected ‘B’ grade.

Another example of a logical error (forgetting BODMAS!)

It is impossible to foresee all situations in which errors occur in a program. As we anticipate these problematic situations, many programming languages provide error handling techniques. X = int(input(‘Please enter your first number’)) Y = int(input(‘Please enter your second number’)) print(X/Y) Error Catching If the user typed in 0 for their second number, we would get a Runtime error! How do we get around this situation?

The solution is to try to ‘catch’ the error.. try: X = int(input(‘Please enter your first number’)) Y = int(input(‘Please enter your second number’)) print(X/Y) Error Catching The word ‘ try: ’ instructs the compiler/interpreter to execute the block of code as normal. It will attempt to execute the code in here first of all.

try: X = int(input(‘Please enter your first number’)) Y = int(input(‘Please enter your second number’)) print(X/Y) Error Catching If there happens to be an error here somewhere (e.g. the end user types in 0 for Y…a runtime error) …the program would normally crash/terminate! We can avoid this by using except:

try: X = int(input(‘Please enter your first number’)) Y = int(input(‘Please enter your second number’)) print(X/Y) except ZeroDivisionError: print(‘You cannot divide by zero!’) Y=1 Error Catching Try this for yourself and see if it works. Note: this category of error exception ‘ ZeroDivisionError ’.

Another exception error worth noting is the ValueError Value Errors: int(‘T’) For when the arguments of a function have invalid values! Solution: try: x=int(input(‘Please enter a number: ‘)) except ValueError: print(‘Invalid entry, please try again’)

try: test_file = open(‘crazy_file_name.txt’,’r’) Text_file.write(‘Hey there!’) except IOError: print(‘This file doesn’t exist!’) test_file.close() the ‘ IOError ’ is when no file exists or can’t be opened. the ImportError i f python cannot find the module e.g. import unknown_module A final excample of a common exception error is the IOError

…finally: Look at the code below. If the file ‘ CrazyFile.txt ’ does not exist, then the IOError exception error will be raised and the except clause will handle the situation and close the file. What if there is no exception error? The file will remain open which is not good for our program. try: my_file = open(‘CrazyFile.txt’, ‘r’) print(my_file.read()) except IOError: print(‘no such file exists!’) my_file.close()

…finally: It turns out that the ‘ try…finally ’ can help solve this situation. try: my_file = open(‘CrazyFile.txt’, ‘r’) try: print(my_file.read()) finally : my_file.close() except IOError: print(‘no such file exists!’) Note: the finally block will be executed regardless of any exception Once the file has been opened successfully by the open function, you want to make absolutely sure that you close it, even if an exception is raised by the seek or read methods. That's what a try...finally block is for: code in the finally block will always be executed, even if something in the try block raises an exception. Think of it as code that gets executed on the way out, regardless of what happened before.

Task 1.Create a program that opens an external text file called animals.txt 2.The contents of this file should be output on the screen. 3.Handle any IOErrors that could potentially occur here. 4.If the user wants to add a new animal, they should type in a number (e.g. 1) Amend your program to allow for this. 5.Refine your program further so that it does not encounter a runtime error if the user enters a letter instead of a number. 6.Your program should then invite the end user to add more animals. These new animals should be added accordingly to the text file, with each new animal on a separate line.