COE318 Lecture Notes Week 10 (Nov 7, 2011)

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

CS Introduction to Data Structures Tuesday, February 2, 2016

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

Programming II (CS300)

Programming II (CS300)

Fundamentals of Object Oriented Programming

Exception-Handling Overview

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

Defensive Programming. Ric Glassey

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

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

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

More on Exception Handling

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

More on Exception Handling

C16b: Exception Handling

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

CS1020 Data Structures and Algorithms I Lecture Note #8. Exceptions Handling exceptional events

Chapter 12 Exception Handling

Exceptions and Error Handling

CS 200 Command-Line Arguments & Exceptions Jim Williams, PhD


Chapter 13 Exception Handling

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

Typecasts and Dynamic Dispatch. Dynamic dispatch

CS159. Nathan Sprague

CSCI 261 Computer Science II

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

Java Exception. Wang Yang

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

Download link: Java Exception Handling

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

16-Dec-10. Consider the following method:

CSC 1214: Object-Oriented Programming

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. CSC207 Winter 2017

Contents. I Introductory Examples. Topic 06 -Exception Handling

Object Oriented Programming Exception Handling

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

ITI Introduction to Computing II

SSE3052: Embedded Systems Practice

Data Structures. 02 Exception Handling

Java Errors and Exceptions. Because Murphy s Law never fails

ITI Introduction to Computing II

School of Informatics, University of Edinburgh

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

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

For more details on SUN Certifications, visit

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

The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT

Object Oriented Software Design

Object Oriented Software Design

PASS4TEST IT 인증시험덤프전문사이트

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

Programming - 2. Common Errors

Full file at Chapter 2 - Inheritance and Exception Handling

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

Sri Vidya College of Engineering & Technology Question Bank

Here is a hierarchy of classes to deal with Input and Output streams.

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

CSC207H: Software Design. Exceptions. CSC207 Winter 2018

COMP-202 Unit 9: Exceptions

CS 302 Week 9. Jim Williams

Introduction to Java. Handout-3a. cs402 - Spring

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

EXCEPTIONS. Prof. Chris Jermaine

Chair of Software Engineering. Java and C# in Depth. Prof. Dr. Bertrand Meyer. Exercise Session 7. Nadia Polikarpova

13: Error Handling with Exceptions. The basic philosophy of Java is that "badly formed code will not be run."

Chapter 14. Exception Handling and Event Handling ISBN

6.Introducing Classes 9. Exceptions

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

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

Introductory Programming Exceptions and I/O: sections

CSC Java Programming, Fall Java Data Types and Control Constructs

Unit 5 - Exception Handling & Multithreaded

ECE 122. Engineering Problem Solving with Java

Types, Values and Variables (Chapter 4, JLS)

CS 3 Introduction to Software Engineering. 3: Exceptions

Software Practice 1 - Error Handling

Chapter 11 Handling Exceptions and Events. Chapter Objectives

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

Objektorienterad programmering

Declarations and Access Control SCJP tips

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

Lecture 19 Programming Exceptions CSE11 Fall 2013

17. Handling Runtime Problems

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

ASSERTIONS AND LOGGING

COE318 Lecture Notes Week 13 (Nov 28, 2011)

Exception Examples. All examples written by Edith Hemaspaandra and modified by Jessica Bayliss

2IP15 Programming Methods

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

CS112 Lecture: Exceptions and Assertions

EXCEPTIONS. Java Programming

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

SECTION-1 Q.1.Which two code fragments are most likely to cause a StackOverflowError? (Choose two.)

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

Chapter 14. Exception Handling and Event Handling

Software Construction

COE318 Lecture Notes Week 8 (Oct 24, 2011)

Transcription:

COE318 Software Systems Lecture Notes: Week 10 1 of 5 COE318 Lecture Notes Week 10 (Nov 7, 2011) Topics More about exceptions References Head First Java: Chapter 11 (Risky Behavior) The Java Tutorial: Read the Chapter on Exceptions. (This also includes several questions/answers and examples. More about Exceptions (The basics) An Exception is thrown when a method (or constructor) encounters a situation it cannot deal with. Some examples of this kind of situation include: Trying to divide by zero. Trying to use a null object. Encountering an invalid parameter, Trying to access an array with an illegal index. Trying to open a non-existent file or URL. Incorrectly casting an object to an illegal type. When a method (or constructor) throws an exception, it aborts execution (and a constructor does not create a new object). The recipient of the Exception is the method that invoked the method that threw the exception. The receiving method can either duck or catch the exception. If the exception is ducked (ignored), it is propagated to the method that called that method. If nothing catches the exception, the entire program is terminated. A block of statements which may cause result in an exception can be placed in a try block and and specific exceptions can be caught in catch blocks.

COE318 Software Systems Lecture Notes: Week 10 2 of 5 Example of basic usage public class A { public void method(a a) { int i = 5; int j; j = i / 1; // j = i / 0; System.out.println("A"); // throw new MyException(); public class B extends A { @Override public void method(a a) { a.method(a); int i = Integer.parseInt("123"); System.out.println("B"); public class C extends B { private static boolean CATCH = false; public void method(b b) { Object obj = (Object) b; // String s = (String) obj; if (!CATCH) { b.method(new A()); else { b.method(new A());

COE318 Software Systems Lecture Notes: Week 10 3 of 5 catch (NullPointerException ex) { System.err.println("Caught C: " + ex); catch (Exception ex) { System.err.println("Caught C: " + ex); System.out.println("methodC works!"); public class Main { private static boolean CATCH = false; public static void main(string[] args) { C c = new C(); if (!CATCH) { c.method(c); else { c.method(c); catch (ArithmeticException ex) { System.err.println("Caught (in main): " + ex); finally { System.out.println("Finally in main"); System.out.println("Main finished successfuly"); public class MyException extends Exception { /** * Creates a new instance of <code>myexception</code> * without detail message. */ public MyException() {

COE318 Software Systems Lecture Notes: Week 10 4 of 5 /** * Constructs an instance of <code>myexception</code> * with the specified detail message. * @param msg the detail message. */ public MyException(String msg) { super(msg); Checked vs. Unchecked Exceptions Exceptions are divided into two broad categories: unchecked (or runtime) exceptions: ordinary ones and Errors checked exceptions Unchecked exceptions include (among others): IllegalArgumentException NullPointerException ArrayIndexOutOfBoundsException ArithmeticException ClassCastException Unchecked exceptions are often (but not always) the result of a programming error (bug). Unchecked exceptions do not have to be declared or caught. Checked exceptions must be declared. The compiler insists! A method that may throw a checked exception must explicitly declare that it does with the throws clause. For example: void methodc() throws MyException {...throw(new MyException(); Any method that invokes methodc must either: catch it; or re-throw it For example, if methodb invokes methodc, we must have one of the two alternatives:

COE318 Software Systems Lecture Notes: Week 10 5 of 5 //Alternative 1: catch it void methodb() {. methodc(); catch (MyException ex) {. //Alternative 2: re-throw it void methodb throws MyException { methodc(); Custom Exceptions You can make your own exceptions. These should be checked exceptions. (i.e. extend Exception.)