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

Similar documents
ECE 122. Engineering Problem Solving with Java

Chapter 13 Exception Handling

Errors and Exceptions

Exceptions in Java

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

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

Exceptions. CSC207 Winter 2017

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

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

BBM 102 Introduction to Programming II Spring Exceptions

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

COMP-202 Unit 9: Exceptions

COMP-202 Unit 9: Exceptions

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

CSC207H: Software Design. Exceptions. CSC207 Winter 2018

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

More on Exception Handling

BBM 102 Introduction to Programming II Spring 2017

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

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

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

Introductory Programming Exceptions and I/O: sections

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

COMP1008 Exceptions. Runtime Error

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

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

Java Programming Unit 7. Error Handling. Excep8ons.

More on Exception Handling

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

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

Introduction to Computer Science II CS S-22 Exceptions

Fundamentals of Object Oriented Programming

CS 3 Introduction to Software Engineering. 3: Exceptions

Object Oriented Programming Exception Handling

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

EXCEPTION-HANDLING INTRIVIEW QUESTIONS

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

Object Oriented Programming. Week 7 Part 1 Exceptions

Download link: Java Exception Handling

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

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

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

CS112 Lecture: Exceptions and Assertions

Lecture 19 Programming Exceptions CSE11 Fall 2013

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

Full file at Chapter 2 - Inheritance and Exception Handling

C16b: Exception Handling

What did we talk about last time? Course overview Policies Schedule

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

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

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

CSCI 261 Computer Science II

Some examples and/or figures were borrowed (with permission) from slides prepared by Prof. H. Roumani. Exception Handling

ITI Introduction to Computing II

Comp 249 Programming Methodology Chapter 9 Exception Handling

Internal Classes and Exceptions

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

ITI Introduction to Computing II

More on Objects in JAVA TM

Programming Languages and Techniques (CIS120)

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

CMSC 202. Exceptions

Correctness and Robustness

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

Exceptions Handling Errors using Exceptions

EXCEPTION HANDLING. Summer 2018

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

CS159. Nathan Sprague

Exceptions - Example. Exceptions - Example

Typecasts and Dynamic Dispatch. Dynamic dispatch

Introduction to Software Design

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

Programming II (CS300)

Exceptions (part 2) An exception is an object that describes an unusual or erroneous situation. Quick Review of Last Lecture.

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

The University of Melbourne Department of Computer Science and Software Engineering Software Design Semester 2, 2003

Exceptions Programming 1 C# Programming. Rob Miles

Exceptions. Exceptions. Exceptional Circumstances 11/25/2013

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

Programming II (CS300)

Object Oriented Programming

Exceptions Chapter 10. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

16-Dec-10. Consider the following method:

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

CS 11 java track: lecture 3

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

COSC 123 Computer Creativity. I/O Streams and Exceptions. Dr. Ramon Lawrence University of British Columbia Okanagan

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

Chapter 12 Exception Handling

COSC Exception Handling. Yves Lespérance. Lecture Notes Week 10 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

Exception in thread "main" java.lang.arithmeticexception: / by zero at DefaultExceptionHandling.main(DefaultExceptionHandling.

Java Errors and Exceptions. Because Murphy s Law never fails

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

Exceptions and Design

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Chapter 6: The C Preprocessor

File I/O and Exceptions

Programming for Mobile Computing

Transcription:

CMSC131 Exceptions and Exception Handling When things go "wrong" in a program, what should happen. Go forward as if nothing is wrong? Try to handle what's going wrong? Pretend nothing bad happened? Crash with meaningless messages? Crash with meaningful messages? 1

"When Good Code Goes Bad" Some languages have an infrastructure for approaching this. In Java, we have Exceptions and Exception Handling. We will say that an exception is "thrown" and that our code might "catch" an exception when we "try" to execute a piece of code than might cause an exception to be thrown. Exceptions What sorts of "bad" things could happen to cause a portion of code to throw (or raise) an exception? Math errors like dividing by 0 (some Java classes do throw an exception) or rolling over the boundary of what values can be stored (Java doesn't raise an exception). You try to follow a reference that doesn't point at anything (Java doesn't really let these compile). You go beyond the boundaries of a data structure (we will see this with arrays later). File-related errors like trying to open one that's not there or write to one that's read-only, or trying to read beyond the end of it. Things specific to the logic of your application (Java has no problem with assigning a date in the future but your program might think that's an error if it is a birth date). 2

Exceptions in Java In Java, an exception is actually an object. The object will typically contain information about the exception such as a message describing the type of exception or even the cause of the exception. It might have stack information to help you trace the error. Types of exceptions in Java include: ArithmeticException IOException NumberFormatException RuntimeException (fairly generic and can be used to hold any of the above and more) Exception (really generic and can essentially be used to hold any type of exception in well-written Java) Exception Handling try { //code that might throw an exception catch(exception e) { //code to deal with an exception being // thrown when running the code finally { //code to run after attempt regardless of // whether or not an exception was thrown 3

Catching Exceptions It is worth noting that an exception does not need to be "caught" right away. If an exception is thrown, the exception will propagate its way back through the call stack until either it is caught or it reaches the top. There is a type of hierarchy of exceptions where some can catch others that they consider part of their umbrella. For example, catching Exception will deal with any type of exception but catching RuntimeException deals with those exceptions as well as things like ArithmeticException but not something like a PrinterException). If any exception reaches the top of a program s call stack without being caught, it program will "crash". Throwing Exceptions We can create and throw our own exceptions in Java (and several other languages). We can use an existing exception type or build our own once we learn about something called inheritance later in the semester. If we throw our own exception and nothing up the stack catches it, the program will "crash" as a result of it. 4

What will happen? int i; try { i = 3.45F; catch (Exception e) { System.out.println("Bad Math!"); 1. Won t compile. 2. Will compile but then crash. 3. Will compile, throw an error, catch it. Catching different exception types try { //LINE(S) OF CODE HERE catch (ArithmeticException e){ System.out.println("A " + e); catch (IOException e) { System.out.println("IO " + e); catch (NumberFormatException e) { System.out.println("NF " + e); catch (IndexOutOfBoundsException e) { System.out.println("B " + e); catch (Exception e) { System.out.println("E " + e); 5

Expected Exceptions In future courses you ll see that there are certain types of exception you expect to happen, know how to handle when they do, and can write programs that deal with them and continue on successfully Copyright 2010-2017 : Evan Golub 6