CS-1020 and Exception Handling

Slides:



Advertisements
Similar presentations
11-Jun-14 The assert statement. 2 About the assert statement The purpose of the assert statement is to give you a way to catch program errors early The.
Advertisements

1. Define the concept of assertions. 1 Explain the use of assertions. 2 Create Java program using assertions. 3 Run Java program using assertions. 4 2.
Detecting Bugs Using Assertions Ben Scribner. Defining the Problem  Bugs exist  Unexpected errors happen Hardware failures Loss of data Data may exist.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 8 Exceptions and Assertions.
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 13 Exception.
 Both System.out and System.err are streams—a sequence of bytes.  System.out (the standard output stream) displays output  System.err (the standard.
SE-1020 Dr. Mark L. Hornick 1 More Exception Handling and Throwing Exceptions.
SE-1020 Dr. Mark L. Hornick 1 Exceptions and Exception Handling.
 2005 Pearson Education, Inc. All rights reserved Exception Handling.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
8-May-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
Exception Handling.  What are errors?  What does exception handling allow us to do?  Where are exceptions handled?  What does exception handling facilitate?
11-Jun-15 Exceptions. 2 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.
The Java Assert Statement. 2 Assert A Java statement in JDK 1.4 & newer Intent: enables code to test assumptions. E.g., a method that calculates a particle’s.
Chapter 8 Exceptions. Topics Errors and Exceptions try-catch throwing Exceptions Exception propagation Assertions.
Exceptions and Assertions Recitation – 03/13/2009 CS 180 Department of Computer Science, Purdue University.
Chapter 12: Advanced Topics: Exception Handling Visual Basic.NET Programming: From Problem Analysis to Program Design.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
1 Exception Handling Introduction to Exception Handling Exception Handling in PLs –Ada –C++ –Java Sebesta Chapter 14.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
Assertions Program correctness. Assertions Java statement – enables you to assert an assumption about your program. – An assertion contains a Boolean.
CPSC 252 Exception Handling Page 1 Exceptions and exception handling Client programmers can make errors using a class attempting to dequeue an item from.
Chapter 12: Exception Handling
17. Python Exceptions Handling Python provides two very important features to handle any unexpected error in your Python programs and to add debugging.
1 Assertions. 2 assertions communicate assumptions about the state of the program, and stop processing if they turn out to be false very often comments.
Exceptions Handling Exceptionally Sticky Problems.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 15 Exceptions and.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
Programming with Assertions © Allan C. Milne v
Pre- and postconditions, Using assertions and exceptions 1 Pre- and postconditions Using assertions and exceptions.
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.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
The Java Assertion. 2 Assertion A Java statement in JDK 1.4 & newer Intent: enables code to test assumptions. E.g., a method that calculates the a particle’s.
1 Chapter 15 Exceptions and Assertions. 2 Objectives F To know what is exception and what is exception handling (§15.2). F To distinguish exception types:
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 15 Exceptions and Assertions Nothing is impossible.
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
IMS 3253: Validation and Errors 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Validation and Error Handling Validation.
Defensive Programming. Good programming practices that protect you from your own programming mistakes, as well as those of others – Assertions – Parameter.
Introduction to OOP with Java 4th Ed, C. Thomas Wu
Chapter 13 Exception Handling
Chapter 6 CS 3370 – C++ Functions.
Logger, Assert and Invariants
Handling Exceptionally Sticky Problems
Exception Handling and Event Handling
Exceptions C++ Interlude 3
Chapter 14: Exception Handling
Programming in Java Assertion.
Exception Handling Chapter 9 Edited by JJ.
Arrays and Collections
Exceptions 19-Feb-19.
ECE 103 Engineering Programming Chapter 56 Runtime Errors
Java Exceptions Dan Fleck CS211.
Exceptions 25-Apr-19.
Exceptions 22-Apr-19.
CSC 143 Java Errors and Exceptions.
Handling Exceptionally Sticky Problems
Additional control structures
Computer Science 340 Software Design & Testing
Exceptions 5-Jul-19.
Looping and Repetition
Defensive Programming
Presentation transcript:

CS-1020 and Exception Handling 5/15/2019 Assertions and Exception Handling Portions adapted with permission from the textbook author. CS-1020 Dr. Mark L. Hornick Dr. Mark L. Hornick

Assertions can be used to help debug your programs The syntax for the assert statement is assert( <boolean expression> ); where <boolean expression> represents the condition that must be true if the code is working correctly. If the expression is false, an AssertionError (a subclass of Error) is thrown. You can’t (or shouldn’t) catch AssertionError’s Portions adapted with permission from the textbook author. CS-1020 Dr. Mark L. Hornick

Assert Example public double fromDollar(double dollar){ assert( exchangeRate > 0.0 ); // throw if <0 return (dollar * exchangeRate); } public double toDollar(double foreignMoney){ assert( exchangeRate > 0.0 );// throw if < 0 return (foreignMoney / exchangeRate); Portions adapted with permission from the textbook author. CS-1020 Dr. Mark L. Hornick

The assert() statement may also take the form: assert(<boolean expr>): <expression>; where <expression> represents the value passed as an argument to the constructor of the AssertionError class. The value serves as the detailed message of a thrown error. Portions adapted with permission from the textbook author. CS-1020 Dr. Mark L. Hornick

Assertions Example public double fromDollar(double dollar){ assert exchangeRate > 0.0: “Exchange rate = “ + exchangeRate + “.\nIt must be a positive value.”; return (dollar * exchangeRate); } Portions adapted with permission from the textbook author. CS-1020 Dr. Mark L. Hornick

Assertions are normally disabled, and are usually only enabled for program testing To run the program with assertions enabled, add the “-ea” argument to the VM in the Run Dialog If the –ea option is not provided, the program is executed without checking assertions. Portions adapted with permission from the textbook author. CS-1020 Dr. Mark L. Hornick

Do not use the assertion feature to ensure the validity of an argument during normal execution Use assertions only to detect internal programming errors while debugging Use exceptions during normal execution to notify client programmers of the misuse of classes. Portions adapted with permission from the textbook author. CS-1020 Dr. Mark L. Hornick