Day 8. COMP1006/1406 Summer M. Jason Hinek Carleton University

Similar documents
Errors and Exceptions

EXCEPTION HANDLING. Summer 2018

ECE 122. Engineering Problem Solving with Java

BBM 102 Introduction to Programming II Spring Exceptions

Exceptions, try - catch - finally, throws keyword. JAVA Standard Edition

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

Exceptions. Readings and References. Exceptions. Exceptional Conditions. Reading. CSE 142, Summer 2002 Computer Programming 1.

BBM 102 Introduction to Programming II Spring 2017

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

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

Exceptions. CSC207 Winter 2017

PIC 20A Exceptions. Ernest Ryu UCLA Mathematics. Last edited: November 27, 2017

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 7

CSC207H: Software Design. Exceptions. CSC207 Winter 2018

Exceptions - Example. Exceptions - Example

Exceptions. Examples of code which shows the syntax and all that

Programming II (CS300)

ITI Introduction to Computing II

CS112 Lecture: Exceptions. Objectives: 1. Introduce the concepts of program robustness and reliability 2. Introduce exceptions

ITI Introduction to Computing II

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

CS 61B Data Structures and Programming Methodology. July 7, 2008 David Sun

CS 3 Introduction to Software Engineering. 3: Exceptions

6.Introducing Classes 9. Exceptions

EXCEPTION-HANDLING INTRIVIEW QUESTIONS

Exceptions Handling Errors using Exceptions

A problem?... Exceptions. A problem?... A problem?... Suggested Reading: Bruce Eckel, Thinking in Java (Fourth Edition) Error Handling with Exceptions

Exception Handling Generics. Amit Gupta

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

Exception-Handling Overview

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

Internal Classes and Exceptions

COMP1008 Exceptions. Runtime Error

CS159. Nathan Sprague

CS112 Lecture: Exceptions and Assertions

Chapter 13 Exception Handling

CSCI 261 Computer Science II

Programming II (CS300)

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

Exceptions in Java

Correctness and Robustness

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

Principles of Software Construction: Objects, Design and Concurrency. Exceptions, scope, static data and methods, and Generics.

Data Structures. 02 Exception Handling

Object Oriented Programming

Object Oriented Programming

Exception Handling. Exception Handling

Program Correctness and Efficiency. Chapter 2

Inheritance. SOTE notebook. November 06, n Unidirectional association. Inheritance ("extends") Use relationship

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

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

Programming Languages and Techniques (CIS120)

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

CSE 331 Software Design & Implementation

Abstract Classes, Exceptions

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

Chapter 14. Exception Handling and Event Handling ISBN

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

Introduction. Exceptions: An OO Way for Handling Errors. Common Runtime Errors. Error Handling. Without Error Handling Example 1

Software Practice 1 - Error Handling

17. Handling Runtime Problems

CS S-22 Exceptions 1. Running a web server, don t want one piece of bad data to bring the whole thing down

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

What is the purpose of exceptions and exception handling? Vocabulary: throw/raise and catch/handle Exception propagation Java checked and unchecked

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

Introduction to Computer Science II CS S-22 Exceptions

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

Assertions and Exceptions Lecture 11 Fall 2005

Checked and Unchecked Exceptions in Java

Good Coding Practices Spring 2018

A Third Look At Java. Chapter Seventeen Modern Programming Languages, 2nd ed. 1

09/08/2017 CS2530 INTERMEDIATE COMPUTING 9/8/2017 FALL 2017 MICHAEL J. HOLMES UNIVERSITY OF NORTHERN IOWA TODAY S TOPIC: Exceptions and enumerations.

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

16-Dec-10. Consider the following method:

Exception Handling CSCI 201 Principles of Software Development

Exception Handling. Chapter 11. Java By Abstraction Chapter 11. Outline What Are Exceptions?

More on Exception Handling

Object Oriented Programming. Week 7 Part 1 Exceptions

Sri Vidya College of Engineering & Technology Question Bank

Reminder. Topics CSE What Are Exceptions?! Lecture 11 Exception Handling

More on Exception Handling

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

Software Practice 1 - Error Handling Exception Exception Hierarchy Catching Exception Userdefined Exception Practice#5

Exceptions and I/O: sections Introductory Programming. Errors in programs. Exceptions

Introductory Programming Exceptions and I/O: sections

CS11 Java. Fall Lecture 4

Exceptions vs. Errors Exceptions vs. RuntimeExceptions try...catch...finally throw and throws

Chapter 12 Exception Handling

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

Exceptions and Libraries

COS226 - Spring 2018 Class Meeting # 21 April 23, 2018

CSE 143 Java. Exceptions 1/25/

Java Errors and Exceptions. Because Murphy s Law never fails

CS Programming I: Exceptions

CS159. Nathan Sprague

Comp 249 Programming Methodology Chapter 9 Exception Handling

Review. Errors. Ø Users may enter data in the wrong form Ø Files may not exist Ø Program code has bugs!*

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

Contents. I Introductory Examples. Topic 06 -Exception Handling

2.6 Error, exception and event handling

Transcription:

Day 8 COMP1006/1406 Summer 2016 M. Jason Hinek Carleton University

today s agenda assignments Assignment 4 is out and due on Tuesday Bugs and Exception handling 2

Bugs... often use the word bug when there is a problem with our program 3

Bugs... 3

Bugs... bug is an error in our code (or hardware) 3

Bugs... bug is an error in our code (or hardware) debugging is a methodical process of finding and reducing the number of bugs 3

Bugs... essentially 3 types of bugs/errors 4

Bugs... essentially 3 types of bugs/errors compile errors runtime errors logical errors 4

Bugs... essentially 3 types of bugs/errors compile errors program does not compile but compiler tells you why syntax errors, type mismatch errors,... runtime errors logical errors 4

Bugs... essentially 3 types of bugs/errors compile errors program does not compile but compiler tells you why syntax errors, type mismatch errors,... runtime errors cannot be determined at compile time sometimes require re-design of code logical errors 4

Bugs... essentially 3 types of bugs/errors compile errors program does not compile but compiler tells you why syntax errors, type mismatch errors,... runtime errors cannot be determined at compile time sometimes require re-design of code logical errors non-syntax mistakes in your code Java cannot detect or explain these errors may be very hard to find... 4

Bugs... essentially 3 types of bugs/errors compile errors program does not compile but compiler tells you why syntax errors, type mismatch errors,... these are your fault runtime errors cannot be determined at compile time sometimes require re-design of code logical errors non-syntax mistakes in your code Java cannot detect or explain these errors may be very hard to find... 4

Bugs... essentially 3 types of bugs/errors compile errors program does not compile but compiler tells you why syntax errors, type mismatch errors,... these are your fault runtime errors cannot be determined at compile time sometimes require re-design of code these may or may not be your fault logical errors non-syntax mistakes in your code Java cannot detect or explain these errors may be very hard to find... 4

Bugs... essentially 3 types of bugs/errors compile errors program does not compile but compiler tells you why syntax errors, type mismatch errors,... these are your fault runtime errors cannot be determined at compile time sometimes require re-design of code these may or may not be your fault logical errors non-syntax mistakes in your code Java cannot detect or explain these errors may be very hard to find... these are definitely your fault 4

Bugs... first we need to discover that a bug exists 5

Bugs... first we need to discover that a bug exists compile errors are found for free 5

Bugs... first we need to discover that a bug exists compile errors are found for free xkcd - 303 5

Bugs... first we need to discover that a bug exists compile errors are found for free test, test, test,... 6

Bugs... first we need to discover that a bug exists compile errors are found for free test, test, test,... println is your friend 6

Bugs... first we need to discover that a bug exists compile errors are found for free test, test, test,... println is your friend next we need to find in your code where they occur 6

Bugs... first we need to discover that a bug exists compile errors are found for free test, test, test,... println is your friend next we need to find in your code where they occur JVM might tell you where code crashed (runtime error) 6

Bugs... first we need to discover that a bug exists compile errors are found for free test, test, test,... println is your friend next we need to find in your code where they occur JVM might tell you where code crashed (runtime error) observable error may have been caused elsewhere though... 6

Bugs... first we need to discover that a bug exists compile errors are found for free test, test, test,... println is your friend next we need to find in your code where they occur JVM might tell you where code crashed (runtime error) observable error may have been caused elsewhere though... play computer! backtrack... 6

Bugs... first we need to discover that a bug exists compile errors are found for free test, test, test,... println is your friend next we need to find in your code where they occur JVM might tell you where code crashed (runtime error) observable error may have been caused elsewhere though... play computer! backtrack... might be easy or hard 6

Bugs... first we need to discover that a bug exists compile errors are found for free test, test, test,... println is your friend next we need to find in your code where they occur JVM might tell you where code crashed (runtime error) observable error may have been caused elsewhere though... play computer! backtrack... might be easy or hard next we fix the bug 6

Bugs... first we need to discover that a bug exists compile errors are found for free test, test, test,... println is your friend next we need to find in your code where they occur JVM might tell you where code crashed (runtime error) observable error may have been caused elsewhere though... play computer! backtrack... might be easy or hard next we fix the bug if we know exactly where the bugs originates, often easy to fix 6

Bugs... first we need to discover that a bug exists compile errors are found for free test, test, test,... println is your friend next we need to find in your code where they occur JVM might tell you where code crashed (runtime error) observable error may have been caused elsewhere though... play computer! backtrack... might be easy or hard next we fix the bug if we know exactly where the bugs originates, often easy to fix serious bugs may require rethinking your code/objects 6

Bugs... part of the problem is that we don t live in a perfect world... people do not follow API specifications (preconditions) people do weird things with your code... files get corrupted you have no control over how people use your code you have no control over the universe your code is running in need to write robust code robustness is the ability of the code to cope with errors during execution and cope with erroneous input (from wiki) 7

Bugs... we could write code to check for all possibilities in our code error-checking is code added to your code to look for bad data error-handling is what you do when bad data is found = everything needs to be an Object in Java we use Exceptions... an Exception is an error that occurs in your code Exception Handling is what you do when an exception is found goal of using exceptions you want your program to die gracefully you want to program to recover from bad data if possible 8

Exceptions... in Java, exceptions are objects the JVM automatically does this, or you explicitly do this in your code, or another method will explicitly do this in Java, thrown exceptions are always caught explicitly caught by your code, or delegated to someone else to be caught, or caught by the JVM if everyone delegates 9

Exceptions... in Java, exceptions are objects the JVM automatically does this, or you explicitly do this in your code, or another method will explicitly do this in Java, thrown exceptions are always caught explicitly caught by your code, or delegated to someone else to be caught, or caught by the JVM if everyone delegates (graceful) (potentially graceful) (ugly) 9

Exceptions... in Java, exceptions are objects Error Class unrecoverable errors java.lang.stackoverflowerror java.lang.outofmemoryerror you do not generally catch these yourself (you fix your code so it doesn t happen again!) 10

Exceptions... in Java, exceptions are objects Exception Class less severe errors (we might be able to recover from these) there are checked and unchecked exceptions 11

Exceptions... in Java, exceptions are objects Exception Class less severe errors (we might be able to recover from these) there are checked and unchecked exceptions checked exceptions (Exception Class) compiler checks that these are explicitly caught (if there is a throw there must be a catch) IllegalAccessException (try to violate access modifier) FileNotFoundException (file is not found) 11

Exceptions... in Java, exceptions are objects Exception Class less severe errors (we might be able to recover from these) there are checked and unchecked exceptions checked exceptions (Exception Class) compiler checks that these are explicitly caught (if there is a throw there must be a catch) IllegalAccessException (try to violate access modifier) FileNotFoundException (file is not found) unchecked exceptions (RuntimeException subclass) compiler does not check for a catch (typically left for JVM to crash program) you should rethink your code to avoid these... ArtithmeticException (divide by zero for example) NullpointerException IndexOutOfBoundsException 11

Exceptions... 12

let s take a break... for 1.2 minutes 13

Exceptions... new Java keywords throws used in method declaration says that this method is delegating this exception try/catch/finally used to execute code and handle exceptions thrown "try" to execute this code... "catch" any thrown exceptions and handle them "finally" execute some code after everything is done throw explicitly throw an exception 14

Throws void openfile(string fname) throws java.io.filenotfoundexception{... } tells the compiler that this method might 15

Throws void openfile(string fname) throws java.io.filenotfoundexception{... } tells the compiler that this method might explicitly throw a java.io.filenotfoundexception object 15

Throws void openfile(string fname) throws java.io.filenotfoundexception{... } tells the compiler that this method might explicitly throw a java.io.filenotfoundexception object or calls a method that might 15

Throws void openfile(string fname) throws java.io.filenotfoundexception{... } tells the compiler that this method might explicitly throw a java.io.filenotfoundexception object or calls a method that might explicitly throw a java.io.filenotfoundexception object 15

Throws void openfile(string fname) throws java.io.filenotfoundexception{... } tells the compiler that this method might explicitly throw a java.io.filenotfoundexception object or calls a method that might explicitly throw a java.io.filenotfoundexception object or calls a method that might 15

Throws void openfile(string fname) throws java.io.filenotfoundexception{... } tells the compiler that this method might explicitly throw a java.io.filenotfoundexception object or calls a method that might explicitly throw a java.io.filenotfoundexception object or calls a method that might explicitly throw a java.io.filenotfoundexception object 15

Throws void openfile(string fname) throws java.io.filenotfoundexception{... } tells the compiler that this method might explicitly throw a java.io.filenotfoundexception object or calls a method that might explicitly throw a java.io.filenotfoundexception object or calls a method that might explicitly throw a java.io.filenotfoundexception object or calls a method that might 15

Throws void openfile(string fname) throws java.io.filenotfoundexception{... } tells the compiler that this method might explicitly throw a java.io.filenotfoundexception object or calls a method that might explicitly throw a java.io.filenotfoundexception object or calls a method that might explicitly throw a java.io.filenotfoundexception object or calls a method that might......... explicitly throw a java.io.filenotfoundexception object 15

Throws void openfile(string fname) throws java.io.filenotfoundexception{... } let s look at an example 16

Try/Catch use throws to delegate exception handling use try and catch to handle the exception yourself 17

Try/Catch use throws to delegate exception handling use try and catch to handle the exception yourself try{ block of code to try } catch(exception e){ block of code to execute if exception is caught } 17

Try/Catch use throws to delegate exception handling use try and catch to handle the exception yourself try{ block of code to try } catch(exception e){ block of code to execute if exception is caught } catch(runtimeexception re){ block of code to execute if exception is caught } Note: this won t compile 17

Try/Catch use throws to delegate exception handling use try and catch to handle the exception yourself let s look at the same example... 18

Try/Catch order matters! class hierarchy matters! java.lang.object java.lang.throwable java.lang.exception java.io.ioexception java.io.filenotfoundexception 19

Try/Catch order matters! class hierarchy matters! Object Throwable Exception java.io.ioexception java.io.filenotfoundexception 19

Try/Catch try{ block of code to try } catch(ioexception ioe){ block of code to execute if exception thrown } catch(runtimeexception re){ block of code to execute if exception thrown } catch(exception e){ block of code to execute if exception thrown } { block of code to try } if (IOException is thrown){ block of code to execute if exception thrown } else if (RuntimeException){ block of code to execute if exception thrown } else if (Exception){ block of code to execute if exception thrown } 20

Try/Catch order matters! class hierarchy matters! Object Throwable Exception java.io.ioexception java.io.filenotfoundexception 21

Try/Catch/Finaly try executes a block of code if exception thrown, catch it and handle it after try and possibly catch code executes, we finally execute some finishing code 22

Try/Catch/Finaly try executes a block of code if exception thrown, catch it and handle it after try and possibly catch code executes, we finally execute some finishing code try{ block of code to try } catch(throwable e){ block of code to execute if exception is caught } 22

Try/Catch/Finaly try executes a block of code if exception thrown, catch it and handle it after try and possibly catch code executes, we finally execute some finishing code try{ block of code to try } catch(throwable e){ block of code to execute if exception is caught } finally{ block of code to execute REGARDLESS of what happens above } // like a funny else 22

Throw throw is the mechanism to explicitly throw an exception. 23

Throw throw is the mechanism to explicitly throw an exception. throw new Exception("something bad here..."); creates a new Exception object and throws it 23

Throw throw is the mechanism to explicitly throw an exception. throw new Exception("something bad here..."); creates a new Exception object and throws it throw e; throws an existing exception (e must be a Throwable object ) 23

Making Your Own Exceptions you create your own custom exceptions by extending an appropriate Thowable class 24

Making Your Own Exceptions you create your own custom exceptions by extending an appropriate Thowable class 24

Making Your Own Exceptions you create your own custom exceptions by extending an appropriate Thowable class public class BadCardRankException extends Exception { } public BadCardRankException(){ super("rank of card is invalid"); } 25

Throwable the Throwable class is the root of all exceptions in Java tostring() returns short description of this object getmessage() returns the message as input with the constructor printstacktrace() prints the stack trace to standard error 26