Introduction to Computer Programming Error Handling.

Slides:



Advertisements
Similar presentations
Exceptions & exception handling Use sparingly. Things you can do with exceptions: 1. Define a new exception class. 2. Create an exception instance. 3.
Advertisements

CS102--Object Oriented Programming
Introduction to Exceptions in Java. 2 Runtime Errors What are syntax errors? What are runtime errors? Java differentiates between runtime errors and exceptions.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 13 Exception Handling.
Exception Handling By: Thomas Fasciano. What is an Exception?  An error condition that occurs during the execution of a Java Program.
Java Programming Exceptions. Java has a built in mechanism for error handling and trapping errors Usually this means dealing with abnormal events or code.
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
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.
Testing and Error Handling Intro to Java. Testing We test to try and make sure our programs work correctly and have no bugs If we have access to the code,
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
File I/O and Exceptions File I/O Exceptions Throwing Exceptions Try statement and catch / finally clauses Checked and unchecked exceptions Throws clause.
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.
CS 206 Introduction to Computer Science II 09 / 14 / 2009 Instructor: Michael Eckmann.
1 Why do we need exceptions? In C, return variables must be used to indicate errors: if((fd = fopen(path,...)) == -1){ if(errno==a){...} else if(errno==b){...}
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
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.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
Exceptions COMP53 Sept Exceptions An exception is an object that gets thrown to indicate an error or other exceptional condition. Using exceptions.
Exception Handling Recitation – 10/(23,24)/2008 CS 180 Department of Computer Science, Purdue University.
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Exception Handling in Java Course Lecture Slides 7 th July 2010 “ Admitting.
Example 1 :- Handling integer values public class Program1 { public static void main(String [] args) { int value1, value2, sum; value1 = Integer.parseInt(args[0]);
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.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 9 : Exception Handling King Fahd University of Petroleum & Minerals College of Computer.
CIS 270—Application Development II Chapter 13—Exception Handling.
Conditional If Week 3. Lecture outcomes Boolean operators – == (equal ) – OR (||) – AND (&&) If statements User input vs command line arguments.
CSC172 Intro Pepper. Goals Reminder of what a class is How to create and use classes How to design classes How to think in terms of objects How to comment.
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.
Handling Exceptions in java. Exception handling blocks try { body-code } catch (exception-classname variable-name) { handler-code }
CSc2310 tutoring session, week 8 Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/23/2012 and 10/24/2012 -Using Exceptions -Homework 4.
Exception. Runtime Error Consider the following program: public class BadArray { public static void main(String[] args) { // Create an array with three.
JSP Exception Handling 20-Oct-15. JSP - E XCEPTION H ANDLING When you are writing JSP code, a programmer may leave a coding errors which can occur at.
COMP Exception Handling Yi Hong June 10, 2015.
Exception Handling in JAVA. Introduction Exception is an abnormal condition that arises when executing a program. In the languages that do not support.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 10.
Question of the Day  A landscaper plants 5 rows of 4 trees each, but only uses 10 trees. How is this possible?
CSC 270 – Survey of Programming Languages C++ Lecture 6 – Exceptions.
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.
Exceptions By the end of this lecture you should be able to: explain the term exception; distinguish between checked and unchecked exception classes in.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Exceptions Handling Exceptions with try and catch The finally-block The throws.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
Exceptions in Java. What is an exception? An exception is an error condition that changes the normal flow of control in a program Exceptions in Java separates.
Chapter 8-Exception Handling/ Robust Programming.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
LECTURE 8: EXCEPTIONS CSC 212 – Data Structures. Error Handling Goals  What should we do when an error occurs?  Should alert system to the error  May.
Question of the Day completes starts  What word completes the 1 st word & starts the 2 nd one: DON???CAR.
Exception and Exception Handling. Exception An abnormal event that is likely to happen during program is execution Computer could run out of memory Calling.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Winter 2006CISC121 - Prof. McLeod1 Last Time Reviewed class structure: –attributes –methods –(inner classes) Looked at the effects of the modifiers: –public.
Lecture 5: Exception Handling and Text File I/O Michael Hsu CSULA.
Exceptions: When things go wrong
CS1101: Programming Methodology Recitation 7 – Exceptions
Something about Java Introduction to Problem Solving and Programming 1.
Exceptions 10-Nov-18.
Handling Exceptions.
مظفر بگ محمدی دانشگاه ایلام
مظفر بگ محمدی دانشگاه ایلام
Exception Handling in Java
Exception Handling in Java
CSC 270 – Survey of Programming Languages
CMSC 202 Exceptions.
Basic Exception Handling
Presentation transcript:

Introduction to Computer Programming Error Handling

What to do with errors? Return something the caller will understand: Public static int myMethod(int x){ if (x < 0){ return -1;} else { do what you want the method to do;} Throw Exception – Public static int myMethod(int x){ if (x < 0){ throw new IllegalArgumentException(x + “is negative and must be 0 or greater”);} else { do what you want the method to do;}

What is an Exception? Java has objects called Exceptions that carry information about errors. IllegalArgumentException is one type When code throws an exception, it stops processing the method without a proper return. When an exception is a “Checked” type, it also stops the program from running if noone deals with it.

Make an Exception Happen import java.util.Scanner; public class Scan { public static int getNumb(){ Scanner rdr = new Scanner(System.in); System.out.println("Enter a number from 1 to 10"); int entry = rdr.nextInt(); return entry; }

What happens to the calling program when an Exception happens? If the caller does not handle it: –Crash –Like your Scanner programs that received letters instead of numbers If the caller handles it: –Does not crash –Does not look at return value

How to code Exception Handling Try/Catch block public static void main(){ int x; try { x = getNumb(); } catch (Exception e) { System.out.println("X was never filled"); } System.out.println("Every line will print here"); }

7 You Try a Try/Catch Block Given this method: public static int times6(int x){ if (x < 0){ throw new IllegalArgumentException(x + "is negative and must be 0 or greater");} else { return 6 * x;} } Code a caller method that takes in 1 number and uses it to call times6. –When the method times6 returns normally, print the returned number. –When the method times6 throws an exception, print “oops!” –Always print “The End”

Answer to Try/Catch practice public static void caller (int a){ try { int y = times6(a); System.out.println(y);} catch (Exception e) { System.out.println("oops!"); } System.out.println("goodbye"); }

Getting the message back When you receive an exception Get the message text with : –Exception object. getmessage() –Example: catch (IllegalArgumentException e) { System.out.println("oops!" + e.getMessage()); }

Checking Types You can throw different types of exceptions Test them in order using elseif logic catch (IllegalArgumentException e) { System.out.println("oops!" + e.getMessage()); } catch(Exception e) {System.out.println("oops2 ");}

Summary You can stop executing a method with a throw command. –Create an exception to throw – new IllegalArgumentException(“tell about the error”); You can catch the error in the calling program using a try /catch block. Try{} Catch{}