Exceptions in Java

Similar documents
CS 3 Introduction to Software Engineering. 3: Exceptions

Exceptions and Design

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

ECE 122. Engineering Problem Solving with Java

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

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

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

Internal Classes and Exceptions

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

EXCEPTION HANDLING. Summer 2018

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

Exception Handling Generics. Amit Gupta

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

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

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

Advanced Flow of Control -- Introduction

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

Lecture 14: Exceptions 10:00 AM, Feb 26, 2018

CSC207H: Software Design. Exceptions. CSC207 Winter 2018

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

Exceptions - Example. Exceptions - Example

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

Introduction to Software Design

Check out FilesAndExceptions from SVN. Exam 2 Review File I/O, Exceptions Vector Graphics Project

BBM 102 Introduction to Programming II Spring Exceptions

Exceptions. CSC207 Winter 2017

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

Object Oriented Programming Exception Handling

Programming Languages and Techniques (CIS120)

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

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

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

BBM 102 Introduction to Programming II Spring 2017

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

Chapter 3 Java Exception

CSE 331 Software Design & Implementation

Exceptions. Text: Chapter19, Big C++

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

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

C16b: Exception Handling

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

CS112 Lecture: Exceptions and Assertions

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

Introductory Programming Exceptions and I/O: sections

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

Programming II (CS300)

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

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

CS61B Lecture #12. Today: Various odds and ends in support of abstraction.

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

Errors. And How to Handle Them

Exception handling & logging Best Practices. Angelin

Introduction to Computation and Problem Solving. Class 25: Error Handling in Java. Prof. Steven R. Lerman and Dr. V. Judson Harward.

Exceptions and assertions

To Think About. MyClass.mogrify(new int[] { 1, 2, 4, 6 }));

Exception-Handling Overview

Pages and 68 in [JN] conventions for using exceptions in Java. Chapter 8 in [EJ] guidelines for more effective use of exceptions.

CSSE 220 Day 19. Object-Oriented Design Files & Exceptions. Check out FilesAndExceptions from SVN

14. Exception Handling

Program Correctness and Efficiency. Chapter 2

COMP1008 Exceptions. Runtime Error

CS193j, Stanford Handout #25. Exceptions

CSSE 220 Day 19. Object-Oriented Design Files & Exceptions. Check out FilesAndExceptions from SVN

CS 251 Intermediate Programming Exceptions

Java Loose Ends. 11 December 2017 OSU CSE 1

CS159. Nathan Sprague

16-Dec-10. Consider the following method:

Exceptions. When do we need exception mechanism? When and why use exceptions? The purpose of exceptions

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

Exceptions. Gunnar Gotshalks EX 1

Linked Lists

ApacheCon NA How to Avoid Common Mistakes in OFBiz Development Presented by Adrian Crum

Chapter 10. Exception Handling. Java Actually: A Comprehensive Primer in Programming

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

Errors and Exceptions

Assertions and Exceptions Lecture 11 Fall 2005

Programming II (CS300)

Lecture 19 Programming Exceptions CSE11 Fall 2013

Sri Vidya College of Engineering & Technology Question Bank

CSE 374 Programming Concepts & Tools

Object Oriented Programming

Chapter 13 Exception Handling

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

Designing Robust Classes

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

Exceptions Programming 1 C# Programming. Rob Miles

Exceptions. Exceptions. Exceptional Circumstances 11/25/2013

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

Programming for Mobile Computing

Exception Handling Introduction. Error-Prevention Tip 13.1 OBJECTIVES

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

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

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

Defensive Programming. Ric Glassey

Recreation. MyClass.mogrify(new int[] { 1, 2, 4, 6 }));

Exceptions. CS162: Introduction to Computer Science II. Exceptions. Exceptions. Exceptions. Exceptions. Exceptions

EXCEPTIONS. Java Programming

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

Supplemental Handout: Exceptions CS 1070, Spring 2012 Thursday, 23 Feb 2012

Transcription:

Exceptions in Java 3-10-2005

Opening Discussion Do you have any questions about the quiz? What did we talk about last class? Do you have any code to show? Do you have any questions about the assignment? When you have a function in which something goes wrong, how does it tell the rest of the program? Do you always put in code to make sure the functions you call execute properly?

Midterm Results The median grade on the exams was an 89. I think I graded too lightly. :) 7 6.5 6 5.5 5 4.5 4 3.5 3 2.5 2 1.5 1 0.5 0 A B C D F

Error Handling In the code that you are probably used to, there are two ways that a function can tell you if an error occurred. Return an error code. Set a flag that should be checked. Both of these are very easy to ignore which can lets the errors propagate and makes debugging much more difficult.

Enter Exceptions An alternate approach to error handling is the use of exceptions. As their name implies, exceptions are things used for exceptional events. When an error occurs, the code will throw an exception. When an exception is thrown control pops up the stack until code is found to handle it. If no handler is found that thread exits completely.

try, catch, and finally Blocks There are 4 syntactic components of dealing with exceptions. The ones you will write most are try and catch blocks. When you have a section of code that can have an exception thrown in it that you want to handle, you put it in a try block. After the try block you can have one or more catch blocks. Each one specifies a type and catches anything of that type (including subtypes). A finally block is like a default. In addition, it ALWAYS happens.

throws and throw Sometimes a method can have an exception occur in it, but it doesn t know how to handle it. In this situation, the exception should be added to the throws clause of the method declaration. If you want to throw your own exceptions you use the throw statement. It is followed by an object that is the exception to be thrown.

Checked vs. Unchecked Exceptions There are two broad categories of exceptions. Checked exceptions must either be caught, or they must appear in the throws clause of the method. Unchecked exceptions don t have to do this and often shouldn t be handled. If an unchecked exception arises it typically signifies a major problem and the code should crash. Subtypes of RuntimeException are unchecked.

Benefits of Exceptions Exceptions are nice because they can t be ignored. They tell you there is a problem immediately instead of letting the code run on until it has a serious problem or just leaving logic errors. The Exception class also has handy methods like printstacktrace() that can be used to help with the debugging process as well.

Code Let s go look in the javadocs at some of the function calls that can throw exceptions then write some code that deals with those exceptions. We can also go put in exceptions in our code that we have written.

Minute Essay The line new FileInputStream(fileName) can throw two exceptions. Look those up in the javadocs, and write code that would catch them and print out semimeaningful messages. Enjoy your Spring Break. The design for assignment #5 is due on the Tuesday when you get back.