Exception Handling. Handling bad user input. Dr. Siobhán Drohan Maireád Meagher. Produced by:

Similar documents
ECE 122. Engineering Problem Solving with Java

Menu Driven Systems. While loops, menus and the switch statement. Mairead Meagher Dr. Siobhán Drohan. Produced by:

Chapter 12 Exception Handling

Java Errors and Exceptions. Because Murphy s Law never fails

Fundamentals of Object Oriented Programming

Internal Classes and Exceptions

Exception-Handling Overview

Topic 6: Exceptions. Exceptions are a Java mechanism for dealing with errors & unusual situations

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 19: NOV. 15TH INSTRUCTOR: JIAYIN WANG

An Introduction to Processing

Correctness and Robustness

C16b: Exception Handling

14. Exception Handling

BBM 102 Introduction to Programming II Spring Exceptions

EXCEPTION HANDLING. // code that may throw an exception } catch (ExceptionType parametername) {

BBM 102 Introduction to Programming II Spring 2017

EXCEPTION HANDLING. Summer 2018

Chapter 13 Exception Handling

Data Structures. 02 Exception Handling

Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling

Chapter 14. Exception Handling and Event Handling

Programming II (CS300)

Errors and Exceptions

EXCEPTIONS. Objectives. The try and catch Statements. Define exceptions. Use try, catch and finally statements. Describe exception categories

Exceptions. Errors and Exceptions. Dealing with exceptions. What to do about errors and exceptions

Programming II (CS300)

About This Lecture. Outline. Handling Unusual Situations. Reacting to errors. Exceptions

CS 3 Introduction to Software Engineering. 3: Exceptions

EXCEPTIONS. Java Programming

CMSC131. Exceptions and Exception Handling. When things go "wrong" in a program, what should happen.

Lecture 20. Java Exceptional Event Handling. Dr. Martin O Connor CA166

COMP-202 Unit 9: Exceptions

Some miscellaneous concepts

Chapter 9. Exception Handling. Copyright 2016 Pearson Inc. All rights reserved.

National University. Faculty of Computer Since and Technology Object Oriented Programming

Designing Robust Classes

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Exceptions. What exceptional things might our programs run in to?

Exception Handling. General idea Checked vs. unchecked exceptions Semantics of... Example from text: DataAnalyzer.

COE318 Lecture Notes Week 10 (Nov 7, 2011)

Exception Handling: Control. Exception handling is the control of error conditions or other unusual events during the execution of a program.

Exception Handling. CSE 114, Computer Science 1 Stony Brook University

Chapter 12 Exception Handling and Text IO. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

JAC444 - Lecture 4. Segment 1 - Exception. Jordan Anastasiade Java Programming Language Course

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Using Methods. Writing your own methods. Dr. Siobhán Drohan Mairead Meagher. Produced by: Department of Computing and Mathematics

COMP200 EXCEPTIONS. OOP using Java, based on slides by Shayan Javed

Input, Output and Exceptions. COMS W1007 Introduction to Computer Science. Christopher Conway 24 June 2003

JUnit Framework. Terminology: assertions, annotations, fixtures. Dr. Siobhán Drohan Mairead Meagher. Produced by:

CS115. Chapter 17 Exception Handling. Prof. Joe X. Zhou Department of Computer Science. To know what is exception and what is exception handling

Exceptions. Produced by. Algorithms. Eamonn de Leastar Department of Computing, Maths & Physics Waterford Institute of Technology

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige

Object Oriented Programming

COMP-202 Unit 9: Exceptions

Object Oriented Programming. Week 7 Part 1 Exceptions

CSC207H: Software Design. Exceptions. CSC207 Winter 2018

Exception Handling. Sometimes when the computer tries to execute a statement something goes wrong:

Why Exceptions? (1.1) Exceptions. Why Exceptions? (1.2) Caller vs. Callee. EECS2030 B: Advanced Object Oriented Programming Fall 2018

Exceptions. EECS2030 B: Advanced Object Oriented Programming Fall 2018 CHEN-WEI WANG

Binary search. int index = Collections.binarySearch(list, element);

Exception Handling. Run-time Errors. Methods Failure. Sometimes when the computer tries to execute a statement something goes wrong:

CS 112 Programming 2. Lecture 08. Exception Handling & Text I/O (1) Chapter 12 Exception Handling and Text IO

Object Oriented Programming Exception Handling

A n exception error occurs when a problem is

Eclipse. JVM, main method and using Eclipse. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

Exceptions. Produced by. Introduction to the Java Programming Language. Eamonn de Leastar

Introduction Unit 4: Input, output and exceptions

Exceptions. CSC207 Winter 2017

Chapter 14. Exception Handling and Event Handling 异常处理和事件处理. 孟小亮 Xiaoliang MENG, 答疑 ISBN

Reading Input from Text File

COMP 213. Advanced Object-oriented Programming. Lecture 17. Exceptions

Iterators. Lecture 24: Iterators & Exceptions. Implementing Iterators. When Good Programs Go Bad! - where! Abstract over control structures (in Clu)!

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11

Motivations. Chapter 12 Exceptions and File Input/Output

Grouping Objects. Primitive Arrays and Iteration. Produced by: Dr. Siobhán Drohan. Department of Computing and Mathematics

Persistence. An Introduction to XML and Serialization. Dr. Siobhán Drohan Maireád Meagher. Produced by:

COMP-202. Exceptions. COMP Exceptions, 2011 Jörg Kienzle and others

Chapter 4.!Data Abstraction: The Walls! 2011 Pearson Addison-Wesley. All rights reserved 4-1

Exceptions. References. Exceptions. Exceptional Conditions. CSE 413, Autumn 2005 Programming Languages

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

EXCEPTIONS. Fundamentals of Computer Science I

Exception Handling. Chapter 11. Outline. Example: The Quotient app What Are Exceptions? Java By Abstraction Chapter 11

CMSC 202. Exceptions

CSCI-140 Midterm Review October 10, 2015 Presented by the RIT Computer Science Community

Chapter 14. Exception Handling and Event Handling ISBN

Sri Vidya College of Engineering & Technology Question Bank

Data Abstraction: The Walls

CS244 Advanced Programming Applications

ITI Introduction to Computing II

CSCI-142 Exam 2 Review November 2, 2018 Presented by the RIT Computer Science Community

Comp 249 Programming Methodology Chapter 9 Exception Handling

Object Oriented Programming

ITI Introduction to Computing II

CS 11 java track: lecture 3

CS 251 Intermediate Programming Exceptions

Recap of OO concepts. Objects, classes, methods and more. Mairead Meagher Dr. Siobhán Drohan. Produced by:

More on Exception Handling

Chapter 8. Exception Handling. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

1z z Java SE 8 Programmer I

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

Transcription:

Exception Handling Handling bad user input Produced by: Dr. Siobhán Drohan Maireád Meagher Department of Computing and Mathematics http://www.wit.ie/

ShopV4.0 (or any version) When testing it, did you try to enter a String instead of an int? e.g. for the Product code? What happened?

ShopV4.0 is NOT robust

ShopV4.0 (or any version) The following code caused a runtime error... double unitcost = input.nextdouble(); This is called a runtime exception. How do we fix this? How do we stop the program from crashing?

What are Exceptions? An Exception is an object that signals that some unusual condition has occurred while the program is executing. Exceptions are intended to be detected and handled, so that the program can continue in a sensible way if at all possible. Java has many predefined Exception objects, and we can also create our own.

When an exception occurs...the normal flow of execution is disrupted and transferred to code, which can handle the exception condition. The exception mechanism is a lot cleaner than having to check an error value after every method call that could potentially fail.

RuntimeException is a subclass of the Exception class encompasses all exceptions which can ordinarily happen at run-time. these exceptions can be thrown by any java statement or a method call. can be avoided through good programming practices! RuntimeException ArithmeticException ArrayIndexOutOfBoundsException NullPointerException Example Causes Can be caused by dividing by zero. Referencing an array index number of 7 when only 5 exist in the array. trying to access an object that has no memory allocated yet.

Catching Exceptions Catching an exception means declaring that you can handle exceptions of a particular class from a particular block of code. You specify the block of code and then provide handlers for various classes of exception. If an exception occurs then execution transfers to the corresponding piece of handler code.

try and catch To catch exceptions, you surround a block of code with a "try, catch" statement. try{ // The try clause is the piece of code which you want to try to execute. // it contains statements in which an exception could be raised } catch (Exception e){ // The catch clauses are the handlers for the various exceptions. //it contains code to handle Exception and recover }

Example of try and catch try{ mymethod(); } catch (Exception e){ System.err.println( Caught Exception: + e) } The parameter e is of type Exception and we can use it to print out what exception occurred.

Flow of control in Exception Handing

ShopV5.0 making our app robust try { System.out.print("Please enter the product code: "); code = input.nextint(); } catch (Exception e) { input.nextline(); //swallows the buffer contents System.out.println("Number expected - you entered text"); }

Improve loop until input valid boolean goodinput = false; //Loop Control Variable while (! goodinput ) { try { System.out.print("Please enter the product code: "); code = input.nextint(); goodinput = true; } catch (Exception e) { input.nextline(); //swallows the buffer contents System.out.println("Num expected - you entered text"); } }

Using do..while boolean goodinput = false; do { try { System.out.print("Please enter the product code: "); code = input.nextint(); goodinput = true; } catch (Exception e) { input.nextline(); //swallows the buffer contents System.out.println("Num expected - you entered text"); } } while (!goodinput);

ShopV5.0 making our app robust What could cause a runtime exception here?

ShopV5.0 making our app robust

ShopV5.0 making our app robust nextint() and nextdouble() are now exception handled!

nextint() and nextdouble() are now exception handled!

ShopV5.0 making our app robust But what about these int reads? Do I have to repeat the same code here? What happens if I add more int reads?

ShopV5.0 making our app robust In order to have DRY code, we should really write a private helper/utility method that can validate our int input. How would we write it?

ShopV5.0 making our app robust For this new method: We need to pass in a prompt string to be printed to the console. And return a valid int.

ShopV5.0 making our app robust Here we are calling the new helper method to read a valid int.

And again, we are calling the new helper method to read a valid int.

ShopV5.0 making our app robust Lets write a helper method now to read a valid double

Department of Computing and Mathematics http://www.wit.ie/