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

Similar documents
Object Oriented Programming. Week 7 Part 1 Exceptions

CSC Java Programming, Fall Java Data Types and Control Constructs

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

COMP1008 Exceptions. Runtime Error

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

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

Object-Oriented Programming

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

More on Exception Handling

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

More on Exception Handling

ECE 122. Engineering Problem Solving with Java

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

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

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Object Oriented Programming

Fundamentals of Object Oriented Programming

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

Comp 249 Programming Methodology Chapter 9 Exception Handling

ITI Introduction to Computing II

Data Structures. 02 Exception Handling

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

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

Exception Handling. Chapter 9

ITI Introduction to Computing II

Std 12 Lesson-10 Exception Handling in Java ( 1

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

COE318 Lecture Notes Week 10 (Nov 7, 2011)

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

School of Informatics, University of Edinburgh

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

JAVA BASICS II. Example: FIFO

Pace University. Fundamental Concepts of CS121 1

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

Exception Handling. General idea Checked vs. unchecked exceptions Semantics of... Example from text: DataAnalyzer.

Lecture 14 Summary 3/9/2009. By the end of this lecture, you will be able to differentiate between errors, exceptions, and runtime exceptions.

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

CH. 2 OBJECT-ORIENTED PROGRAMMING

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

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

CS112 Lecture: Exceptions and Assertions

CMSC 202. Exceptions

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() {

17. Handling Runtime Problems

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

Introductory Programming Exceptions and I/O: sections

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

Programming II (CS300)

Defensive Programming

Object Oriented Programming

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

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

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

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

School of Informatics, University of Edinburgh

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

Chapter 12: Exceptions and Advanced File I/O

Java Programming Unit 7. Error Handling. Excep8ons.

Programming II (CS300)

6.Introducing Classes 9. Exceptions

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Internal Classes and Exceptions

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

Exceptions and Error Handling

15CS45 : OBJECT ORIENTED CONCEPTS

What can go wrong in a Java program while running?

Chapter 11: Exceptions and Advanced File I/O

Exception Handling in Java. An Exception is a compile time / runtime error that breaks off the

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

Java Loose Ends. 11 December 2017 OSU CSE 1

Java Intro 3. Java Intro 3. Class Libraries and the Java API. Outline

Exceptions Handeling

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

Introduction to Programming Using Java (98-388)

Chapter 4 Defining Classes I

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

C16b: Exception Handling

B2.52-R3: INTRODUCTION TO OBJECT ORIENTATED PROGRAMMING THROUGH JAVA

Exceptions, Case Study-Exception handling in C++.

Recitation 3. 2D Arrays, Exceptions

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

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

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

Introduction Unit 4: Input, output and exceptions

Lecture 19 Programming Exceptions CSE11 Fall 2013

EXCEPTION HANDLING. Summer 2018

Exception-Handling Overview

In this lab we will practice creating, throwing and handling exceptions.

Programming Languages and Techniques (CIS120)

Errors and Exceptions

Final Examination Semester 2 / Year 2012

Lecture 28. Exceptions and Inner Classes. Goals. We are going to talk in more detail about two advanced Java features:

Workbook 5. Remember to check the course website regularly for announcements and errata: Managing errors with Java Exceptions

Sri Vidya College of Engineering & Technology Question Bank

Designing Robust Classes

BBM 102 Introduction to Programming II Spring Exceptions

CS 61B Discussion 5: Inheritance II Fall 2014

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

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

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

Transcription:

The University of Melbourne Department of Computer Science and Software Engineering 433-254 Software Design Semester 2, 2003 Answers for Tutorial 7 Week 8 1. What are exceptions and how are they handled in Java? Give an example. If a Java program attempts to carry out an illegal operation at runtime, it does not necessarily halt its processing at that point. In most cases, the Java Virtual Machine provides the possibility of catching the problem and recovering from it. To emphasize that such problems are not fatal, the term exception is used rather than error. Java exception handling provides a systematic way for the user to respond to runtime errors and to decide on an appropriate response rather than simply letting the program come to a halt. Without having an exception handling system built-in to the language, you would have to write your own routines to validate data and handle all the other possible runtime situations before they cause a halt in your program. For example, in the following code we create a special method to test if a String holds a valid integer value before the string goes to the parseint method. String str = getparameter("dat"); if ( testifint(str)) Integer.parseInt(str); else ErrorFlag = MY_ERROR_BAD_FORMAT; return -1; boolean testifint(string str) code to test characters for numbers.. Fortunately, we can instead use the Java trycatch(exception e) operation to handle exceptions. In the following code segment, for example, we surround the parseint method invocation with a try-catch pair. try int data = Integer.parseInt(getParameter("dat"),10);

afunctionsetup(data); catch ( NumberFormatException e) data = -1; Thus, if the string passed to parsint does not represent a valid integer number, parseint throws an instance of the class NumberFormatException, which will cause the program execution to jump to the first line in the catch block. The parseint method in class Integer is written something like the following: public static int parseint(string s, int radix) throws NumberFormatException code to check if the string is a number and if it isn't then: throw new NumberFormatException(s); The throws NumberFormatException phrase in the method signature indicates that the method includes a throw statement for this exception. We see that throw statement actually creates an instance of the exception and causes the routine to return with the exception thrown. The constructors for an exception may include arguments with which you can pass useful information about the circumstances of the exception. The catch code can then examine this info using methods for the particular type of exception. Java divides exceptions into two categories: 1. General exceptions 2. Run-time exceptions The general exceptions must be handled, i.e. a try-catch must either be nested around the call to the method that throws the exception or the method must explicitly indicate with throws that it can generate this exception. (Then other methods that invoke this method must catch the exception.) The compiler will throw an error message if it detects an uncaught exception and will not compile the file. The run-time exceptions do not have to be caught. This avoids requiring that a try-catch be place around, for example, every integer divide

operation to catch a divide by zero or around every array variable to watch for indices going out of bounds. However, you should handle possible run-time exceptions if you think there is a reasonable chance of one occurring. You can use multiple catch clauses to catch the different kinds of exceptions that code can throw as shown in this snippet: You can use multiple catch clauses to catch the different kinds of exceptions that code can throw as shown in this snippet: try some code catch ( NumberFormatException e) catch (IOException e) catch (Exception e) finally // optional this code always executed even if no exceptions Here there are two catch statements for Exception subclasses and one for any other Exception instance. Regardless of whether the code generates an exception or not, the finally code block will be executed. Exception handling is thus based on try-catch operation, the throws and throws keywords, and the Exception class and its subclasses. 2. While reading a file, how would you check whether you reached the end of the file? Some methods simply return -1 (e.g. read() in class DataInputStream or class FileReader) if there are no more data because the end of stream has been reached.

Some other methods throw EOFException (e.g. readfully(byte[] b) in class DataInputStream), if they reach end of file before completing their read. 3. How do we design, create, and access a package? Discuss with suitable example. A package is the Java version of a library. A package refers simply to a group of related class files in the same directory. Each class file, however, includes a package directive with that directory name at the top of the file. For example, say that we put the files TestA.java and TestB.java into the directory mypack, which is a sub-directory of myapps. So on a Windows platform the file path looks like and c:\myapps\mypack\testa.java c:\myapps\mypack\testb.java. At the top of each file we put the statement as shown in the following code: myapps/mypack/testa.java public class TestA public int a; public TestA(int arg1) a = arg1; myapps/mypack/testb.java public class TestB public double x; public TestB(double y) x = y; The import directive tells the compiler where to look for the class definitions when it comes upon a class that it cannot find in the default java.lang package. A class that wants to potentially use any of the classes from mypack package (i.e. TestA and TestB classes) may use an import statement like: import mypack.*;

But, if the class which is trying to call our TestA and/or TestB classes is not in myapps directory then we need to tell the compiler were to look for mypack package by including its absolute path into CLASSPATH environment variable: set CLASSPATH=c:\myApps;$CLASSPATH 4. Discuss the various levels of access protection for packages and their implications? Basically, access control levels for packages follows the general access control rules in Java, which are summerised in the following table: Specifier private class subclass package world X protected X X* X public X X X X package X X The first column indicates whether the class itself has access to the member defined by the access specifier. As you can see, a class always has access to its own members. The second column indicates whether subclasses of the class (regardless of which package they are in) have access to the member. The third column indicates whether classes in the same package as the class (regardless of their parentage) have access to the member. The fourth column indicates whether all classes have access to the member. For more detailed discussion of access control levels and rules, please refer to: http://java.sun.com/docs/books/tutorial/java/javaoo/accesscontrol.html