May 14, 2002Serguei A. Mokhov, 1 Kickstart Intro to Java Part II COMP346/5461 - Operating Systems Revision 1.5 July 23, 2003.

Slides:



Advertisements
Similar presentations
Exceptions CSE301 University of Sunderland Harry Erwin, PhD.
Advertisements

Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
Yoshi
CS102--Object Oriented Programming
Exception Handling Chapter 15 2 What You Will Learn Use try, throw, catch to watch for indicate exceptions handle How to process exceptions and failures.
Errors and Exceptions The objectives of this chapter are: To understand the exception handling mechanism defined in Java To explain the difference between.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 13 Exception Handling.
Exception Handling Xiaoliang Wang, Darren Freeman, George Blank.
 Both System.out and System.err are streams—a sequence of bytes.  System.out (the standard output stream) displays output  System.err (the standard.
Exceptions and Exception Handling Carl Alphonce CSE116 March 9, 2007.
Chapter 8Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 8 l Basic Exception Handling »the mechanics of exceptions l.
Exception Handling1. 2 Exceptions  Definition  Exception types  Exception Hierarchy  Catching exceptions  Throwing exceptions  Defining exceptions.
MIT-AITI Lecture 14: Exceptions Handling Errors with Exceptions Kenya 2005.
Exceptions Any number of exceptional circumstances may arise during program execution that cause trouble import java.io.*; class IOExample { public static.
Exceptions and Exception Handling Carl Alphonce CSE116.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
1 Java - Threads A thread is an individual flow of control within a larger program. A program which is running more than one thread is said to be multithreaded.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
06 - Exceptions. 2 ©S. Uchitel, 2004 A familiar sight? Bluescreen.scr.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
Introduction to Java Chapter 11 Error Handling. Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle.
1 Exception and Event Handling (Based on:Concepts of Programming Languages, 8 th edition, by Robert W. Sebesta, 2007)
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
CS203 Java Object Oriented Programming Errors and Exception Handling.
Java Software Solutions Foundations of Program Design Sixth Edition
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
June 14, 2001Exception Handling in Java1 Richard S. Huntrods June 14, 2001 University of Calgary.
Exception Handling 1. Introduction Users may use our programs in an unexpected ways. Due to design errors or coding errors, our programs may fail in unexpected.
Object Oriented Programming
CIS 270—Application Development II Chapter 13—Exception Handling.
Chapter 12: Exception Handling
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
Slides Credit Umair Javed LUMS Web Application Development.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
May 9, 2002Serguei A. Mokhov, 1 Kickstart Intro to Java Part I COMP346/ Operating Systems Revision 1.6 February 9, 2004.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
VB.Net - Exceptions Copyright © Martin Schray
COMP Exception Handling Yi Hong June 10, 2015.
Exception Handling Unit-6. Introduction An exception is a problem that arises during the execution of a program. An exception can occur for many different.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
Practice Session 9 Exchanger CyclicBarrier Exceptions.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
Computer Programming with JAVA Chapter 8. Exception Handling Basic Exception Handling the mechanics of exceptions Defining and Using Exceptions some "simple"
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
Exceptions in Java. What is an exception? An exception is an error condition that changes the normal flow of control in a program Exceptions in Java separates.
Exception-Handling Fundamentals  A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of.
Copyright © Curt Hill Error Handling in Java Throwing and catching exceptions.
Exceptions Handling Prepared by: Ligemm Mae del Castillo.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
Java Programming: Exceptions1 Exceptions Reference: java.sun.com/docs/books/tutorial/essential/exceptions/
Exceptions Lecture 11 COMP 401, Fall /25/2014.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Exception and Exception Handling. Exception An abnormal event that is likely to happen during program is execution Computer could run out of memory Calling.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Concurrent Programming in Java Based on Notes by J. Johns (based on Java in a Nutshell, Learning Java) Also Java Tutorial, Concurrent Programming in Java.
Agenda Introduction Errors and Exception Exception Hierarchy Classification of Exceptions Built in Exceptions Exception Handling in Java User defined.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Chapter 10 – Exception Handling
Multithreading.
Java Exceptions Dan Fleck CS211.
Java Basics Exception Handling.
CMSC 202 Exceptions.
Exception Handling.
Presentation transcript:

May 14, 2002Serguei A. Mokhov, 1 Kickstart Intro to Java Part II COMP346/ Operating Systems Revision 1.5 July 23, 2003

May 14, 2002Serguei A. Mokhov, 2 Topics Arrays Basics of Inheritance in Java Threads & Scheduling Exception Handling Examples

May 14, 2002Serguei A. Mokhov, 3 Creating Arrays Two ways: –Using new : int aMyIntArray[] = new int[10]; NOTE: creating an array does not imply creating objects for the elements of the array! Objects must be explicitly created! In our case, we have a primitive data type int, so it gets initialized to 0, in case of an object it is null. –Using initializer: int aMyIntArray[] = {1,2,3,4,5,6,7,8,9,10}; This creates an array dynamically and initializes it with the values. NOTE: this is different from C/C++ because values in the initializer in Java can be dynamic, whereas in C/C++ they are always constant.

May 14, 2002Serguei A. Mokhov, 4 “Anonymous” Arrays and Objects “Anonymous” arrays don’t have a name and can be created dynamically within the initializer: Same way any allocated objects can be anonymous. In C/C++ that would be a memory leak. countArgs( new String[] {“Bar”, “Ban”, “Ech”} );

May 14, 2002Serguei A. Mokhov, 5 Multidimensional Arrays Implemented as arrays-of-arrays, so arrays aren’t necessarily rectangular. Syntax: byte a64bytes[][] = new byte[8][8]; No need to specify all dimensions right away: int aCube[][][] = new int[7][][]; –Basically, we have 7 arrays of type int[][]. –Later on the new dimensions will be there after proper initialization. This is illegal, however: int aCube[][][] = new int[5][][2]; Example: TriangleArray.java

May 14, 2002Serguei A. Mokhov, 6 Triangle Array Example Explained After we execute this line: float aTriangle[][] = new float[7][]; we have the following: where a dot ( ) stands for null

May 14, 2002Serguei A. Mokhov, 7 Triangle Array Example Explained (2) Then, gradually, with every iteration, the array becomes multiple-dimensioned: [0.0] X [1.0][2.0] [2.0][3.0][4.0] [3.0][4.0][5.0][6.0] [4.0][5.0][6.0][7.0][8.0] [5.0][6.0][7.0][8.0][9.0][10.0] [6.0][7.0][8.0][9.0][10.0][11.0][12.0]

May 14, 2002Serguei A. Mokhov, 8 Accessing Array Elements Just like in C/C++ If an attempt is made to access the array outside boundaries, an exception ArrayIndexOutOfBoundsException is thrown, and can be caught and dealt with during run-time. More about exceptions later in the tutorial.

May 14, 2002Serguei A. Mokhov, 9 Basics of Inheritance in Java No multiple inheritance. class Foo extends Bar {} means Foo inherits Bar. If no extends given, then the object is always assumed to inherit from the Object class. By using super() in the child’s class we can call parent’s constructor.

May 14, 2002Serguei A. Mokhov, 10 Threads & Scheduling Recall from COMP229 what threads are –One parent process, thus code and resources are the same. –Own stack and registers state, perhaps some private data. Java is a multithreaded language and has a very easy to use thread package ( java.lang.Thread class ) unlike C/C++ JVM 1.2.* and earlier use priority-based scheduling and a Round Robin policy for the threads of the same priority. Later versions of the JVM let the OS schedule the threads (which is particularly useful on multiple CPUs)

May 14, 2002Serguei A. Mokhov, 11 java.lang.Thread This class provides facilities to start and stop threads, assign priorities, etc. There’s a built-in support for the synchronized methods and blocks of code using the synchronized keyword. This means only one thread at a time should run the code marked as synchronized.

May 14, 2002Serguei A. Mokhov, 12 Most Often Used Method of the Thread class (1) start() When you create an object of a subclass Thread it doesn’t do anything useful, just wastes space. In order for the created thread to begin execution and do some useful work, the parent thread has to invoke the start() method. run() Upon call to start() by the parent, the created thread starts execution the body of the run() method, which is provided by the user in the subclassed thread object.

May 14, 2002Serguei A. Mokhov, 13 Most Often Used Method of the Thread Class (2) yield() Among the threads of the same priority, if one thread wishes voluntarily relinquish the CPU and let another thread go ahead, it calls yield(). Using yield() instruction in any form is sometimes called cooperative scheduling. We might use it to simulate a some sort of an “interrupt” at a given point of the thread’s execution path. join() An analogy to the wait() system call in C. A parent thread calls join() on a child thread object if it wants to pause and wait until the child terminates.

May 14, 2002Serguei A. Mokhov, 14 Threads Example Debrief threads operate on the common stack concurrently. Concurrency implies atomicity; otherwise, we get unexpected results. Pseudo code: acquire block: {block = stack[top]; (!) top--;} release block: {top++; (!) stack[top] = block;} Block.java – explained

May 14, 2002Serguei A. Mokhov, 15 Another Way of Creating Threads … is to implement interface Runnable. When is it needed? Just when you need a Thread behaviour, but you cannot subclass Thread because your object already extends from something. Syntax: class myClass extends OtherClass implements Runnable {} You will need to define run() with no arguments of that class. Then do: –Thread myObj = new myClass(); –myObj.start();

May 14, 2002Serguei A. Mokhov, 16 Java’s Exception Handling Mechanism A very significant feature of Java. Beware: It is similar in a way to that of C++, but not the same! Some terminology: –Exception is a signal indicating that something exceptional happened (whatever exceptional means), usually an error. –Throwing an exception means signaling that exceptional condition. –Catching an exception is to react to the signal an do whatever necessary to recover from it (i.e. to handle an exception).

May 14, 2002Serguei A. Mokhov, 17 Exception Propagation Lexical block structure of Java method first; Up the method call stack Next higher enclosing code block … Invoking method If never caught – to main() => JVM exits with an error message and a stack trace.

May 14, 2002Serguei A. Mokhov, 18 Advantages of Using Exceptions Logical way of dealing with regular exceptions and errors by grouping the handling code in one place. Clarity of algorithms and code in your programs. Less error-prone code (sometimes catching an exception is mandatory and enforced by Java).

May 14, 2002Serguei A. Mokhov, 19 Exception Objects All Exceptions are derived from the class Throwable (java.lang.Throwable), which has two standard subclasses: –Error (usually fatal exceptions, which should not/cannot be caught, e.g. out of memory, linkage, dyn. loading, etc) –Exception (usually recoverable, file ops, array bounds, etc) The Exceptions are also Objects thus have some methods and data members. E.g. getMessage() returns a human-readable error that occurred.

May 14, 2002Serguei A. Mokhov, 20 Handling Exceptions Three statements: try / catch / finally try {...} Does nothing but indicates a block of code which is more likely to have exceptions. catch(Exception e) {...} try is followed by zero or more catch statements to handle specified exceptions finally {...} catch is optionally followed by one finally block, which does clean up and is always guaranteed to execute regardless how the try and catch blocks exit (only System.exit() is not part of that rule).

May 14, 2002Serguei A. Mokhov, 21 Declaring Exceptions To throw “normal exceptions”: public void writeFile() throws IOException {} public void break_window() throws BrokenWindowException {} “Normal” means not subclasses of either Error or RuntimeException C++ diff: Java uses throws and not throw

May 14, 2002Serguei A. Mokhov, 22 Defining and Generating Exceptions throw new MyException(“oh dear…”); The application stops and looks for catch statements; if not found, it propagates the exception to higher levels as described before. ExceptionExample.java

May 14, 2002Serguei A. Mokhov, 23 Links and References Official Java site: Java in a Nutshell, Second Edition by David Flanagan, (C) 1997 O’Reily & Associates, Inc. ISBN: X