1 More on Threads b b A section of code executed independently of other threads written within a single program. b b Java threads can access global data;

Slides:



Advertisements
Similar presentations
Chapter 17 Failures and exceptions. This chapter discusses n Failure. n The meaning of system failure. n Causes of failure. n Handling failure. n Exception.
Advertisements

Pearson Education, Inc. All rights reserved. 1.. Exception Handling.
Java Language Quick-Reference Guide B. Oracle10g: Java Programming B - 2 Console Output Java applications and applets can output simple messages to the.
Topics Introduction Types of Errors Exceptions Exception Handling
Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
Exception Handling. Introduction Errors can be dealt with at place error occurs –Easy to see if proper error checking implemented –Harder to read application.
Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer.
CSE 1302 Lecture 21 Exception Handling and Parallel Programming Richard Gesick.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
Objectives In this chapter you will: Learn what an exception is Learn how to handle exceptions within a program See how a try / catch block is used to.
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
Exception Handling 1 CISC6795, Spring Introduction 2 Exception – an indication of a problem that occurs during a program’s execution, for examples:
 2002 Prentice Hall, Inc. All rights reserved. Chapter 14 – Exception Handling Outline 14.1 Introduction 14.2 When Exception Handling Should Be Used 14.3.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Exception Handling. Introduction An exception is an abnormal condition that arises in a code sequence at run time. In computer languages that do not support.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
CSI 3120, Exception handling, page 1 Exception and Event Handling Credits Robert W. Sebesta, Concepts of Programming Languages, 8 th ed., 2007 Dr. Nathalie.
1 Lecture 4 Exception Handling. 2 Exception-Handling Fundamentals An exception is an abnormal condition that arises in a code sequence at run time A Java.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
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.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 14 - Exception Handling Outline 14.1Introduction 14.2When Exception Handling Should Be Used 14.3Other.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Exception Handling An Exception is an indication of a problem that occurs during a program’s execution. Exception handling enables the programmer to create.
Java ThreadsGraphics Programming Graphics Programming: Java Threads.
Java Review 2 – Errors, Exceptions, Debugging Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 11: Handling Exceptions and Events J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Fourth.
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.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
1 Chapter 14 - Exception Handling Outline 14.1Introduction 14.2When Exception Handling Should Be Used 14.3Other Error Handling Techniques 14.4The Basics.
Chapter 12 Inheritance and Exceptions Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,
Object Oriented Programming
06 Exception Handling. 2 Contents What is an Exception? Exception-handling in Java Types of Exceptions Exception Hierarchy try-catch()-finally Statement.
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.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Slides Credit Umair Javed LUMS Web Application Development.
Computer Science [3] Java Programming II - Laboratory Course Lab 3-1: Creating and Using Interfaces Exception Faculty of Engineering & IT Software Engineering.
Java Programming: Guided Learning with Early Objects
VB.Net - Exceptions Copyright © Martin Schray
Introduction to Exception Handling and Defensive Programming.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
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.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 14 - Exception Handling Outline 14.1Introduction 14.2When Exception Handling Should Be Used 14.3Other.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
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.
Exceptions Handling Prepared by: Ligemm Mae del Castillo.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Exception Handling How to handle the runtime errors.
Exception. Agenda Exception. Handling Exceptions. The finally Clause.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Agenda Introduction Errors and Exception Exception Hierarchy Classification of Exceptions Built in Exceptions Exception Handling in Java User defined.
Appendix H Exception Handling: A Deeper Look
Chapter 14 – Exception Handling
Java Programming Language
Lecture 11 Objectives Learn what an exception is.
Java Programming: From Problem Analysis to Program Design, 4e
Presentation transcript:

1 More on Threads b b A section of code executed independently of other threads written within a single program. b b Java threads can access global data; local variables are private to each thread. b b Multithreading: Concurrency.

2 Implementing Threads i) The interface Runnable specifies an abstract method run() which typically will be overridden in a class implementing this interface. public test implements Runnable{ public void run(){ //body of intended thread here }

3 Implementing Threads b b Test is not a thread instance, so need code such as: test testing = new test(); Thread t = new Thread(testing); t.start; // to start thread executing b b call to start will automatically invoke run().

4 Implementing Threads ii) use: class test extends Thread then can create instance of test by writing test eg = new test(); eg.start();

5 Exception Handling b Mechanisms similar to that used in C++ b Some common Exceptions: out-of bounds array subscriptout-of bounds array subscript arithmetic overflow (number to large to be represented)arithmetic overflow (number to large to be represented) division by zerodivision by zero invalid method parametersinvalid method parameters memory exhaustionmemory exhaustion

6 Exception Handling b Java exception handling enables a program to catch all types of exceptions, orall types of exceptions, or all exceptions of a certain type, orall exceptions of a certain type, or all exceptions of related typesall exceptions of related types b Exception handling is a recovery mechanism (recovery from malfunctioning)

7 Exception Handling  Java has a class called Exception. b Exception subclasses can be derived from this class. b When a method detects an error and the method is unable to deal with it, it throws an exception. b This exception is caught only if an exception handler is present.

8 Exception Hadling b try block - used to enclose a code that may generate an exception b catch block - specifies the type of code to be caught and contains an exception handler. b finally block (optional) - provides code that always executes regardless of whether or not exception occurs (use when there are no catch blocks)

9 Exception Handling b throws - clause to specify the exceptions a method throws.

10 Exception Handling - example public class DivideByZeroException extends ArithmeticException { public DivideByZeroException() { super(“Attempted to divide by zero”); //explicit call to superclass constructor }}

11 Exception Handling … try { //code that may generate divide by zero excp. } catch (DivideByZeroException e) { showStatus(e.toString());}….

12 Sockets b Java offers socket-based-communications for network programming b An application can read from/write to a socket (just like file input output) b Java provides (java.net package) stream sockets (for a process to establish a connection to another process - using TCP)stream sockets (for a process to establish a connection to another process - using TCP) datagram sockets (to transmit individual packets - UDP is used).datagram sockets (to transmit individual packets - UDP is used).

13 Interfaces b An interface is a special type of class that is only useful in conjunction with the implements statement b An interface extends the capabilities of a class. public class aClass extends Applet implements Runnable, ActionListener { ……..}

14 Interfaces Interface ImageUtilities { void darken(); void lighten(); void flip(int degrees); } class Test implements ImageUtilities{ …. }