CSE 1302 Lecture 21 Exception Handling and Parallel Programming Richard Gesick.

Slides:



Advertisements
Similar presentations
Pearson Education, Inc. All rights reserved. 1.. Exception Handling.
Advertisements

Topics Introduction Types of Errors Exceptions Exception Handling
Error Handling in.NET Exceptions. Error Handling Old way (Win32 API and COM): MyFunction() { error_1 = doSomething(); if (error_1) display error else.
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.
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.
 2006 Pearson Education, Inc. All rights reserved. Exception Handling in C++ CS-2303, C-Term Exception Handling in C++ CS-2303 System Programming.
Exception Handling Xiaoliang Wang, Darren Freeman, George Blank.
Exceptions and Exception Handling Carl Alphonce CSE116 March 9, 2007.
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Exception Handling: A Deeper.
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.
C++ Exception Handling
1 CSC241: Object Oriented Programming Lecture No 28.
CS 3260 Dennis A. Fairclough Version 1.0
Introduction to Programming Lesson 1. Objectives Skills/ConceptsMTA Exam Objectives Understanding Computer Programming Understand computer storage and.
Understand Error Handling Software Development Fundamentals LESSON 1.4.
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 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;
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.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
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.
 2006 Pearson Education, Inc. All rights reserved Exception Handling.
1 Exception and Event Handling (Based on:Concepts of Programming Languages, 8 th edition, by Robert W. Sebesta, 2007)
 2009 Pearson Education, Inc. All rights reserved Exception Handling.
 2006 Pearson Education, Inc. All rights reserved Exception Handling.
1 Exception Handling Introduction to Exception Handling Exception Handling in PLs –Ada –C++ –Java Sebesta Chapter 14.
June 14, 2001Exception Handling in Java1 Richard S. Huntrods June 14, 2001 University of Calgary.
Dr. Abraham. Exception Any problem that VB or OS could not handle Robust program A program that performs well not only under ordinary conditions but also.
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.
1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about.
Object Oriented Programming
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
Chapter 12: Exception Handling
COMPUTER PROGRAMMING 2 Exceptions. What are Exceptions? Unexpected events that happen when the code is executing (during runtime). Exceptions are types.
1.  Collections are data structures that holds data in different ways for flexible operations  C# Collection classes are defined as part of the ◦ System.Collections.
VB.Net - Exceptions Copyright © Martin Schray
Lecture 21 Parallel Programming Richard Gesick. Parallel Computing Parallel computing is a form of computation in which many operations are carried out.
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.
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.
Exception Handling Programmers must deal with errors and exceptional situations: User input errors Device errors Empty disk space, no memory Component.
HANDLING EXCEPTIONS Chapter 9. Outline  Learn about the limitations of traditional error-handling methods  Throw exceptions  Use try blocks  Catch.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
Exception Handling in C++. Outline What exceptions are and when to use them Using try, catch and throw to detect, handle and indicate exceptions, respectively.
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
Copyright © Curt Hill Error Handling in Java Throwing and catching exceptions.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
C# Exceptions 1 CNS 3260 C#.NET Software Development.
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.
Introduction to Programming Lesson 1. Algorithms Algorithm refers to a method for solving problems. Common techniques for representing an algorithms:
Garbage Collection It Is A Way To Destroy The Unused Objects. To do so, we were using free() function in C language and delete() in C++. But, in java it.
Lecture 18B Exception Handling and Richard Gesick.
Lecture 11 Dr. Eng. Ibrahim El-Nahry Exception Handling.
Object Throwable ErrorException RuntimeException.
Eighth Lecture Exception Handling in Java
Exception Handling in C++
16 Exception Handling.
Java Programming Language
Why exception handling in C++?
Exception Handling and
EE422C Software Implementation II
Part B – Structured Exception Handling
Fundaments of Game Design
Introduction to Programming
Lecture 20 Parallel Programming CSE /27/2019.
Lecture 20 Parallel Programming CSE /8/2019.
Presentation transcript:

CSE 1302 Lecture 21 Exception Handling and Parallel Programming Richard Gesick

Exception Handling An exception is an indication of a problem that occurs during a program’s execution. Exception handling enables applications to resolve exceptions. Exception handling enables clear, robust and more fault-tolerant programs. Exception handling helps improve a program’s fault tolerance. 14-2

Exception Handling 1-3 ArithmeticException A base class for exceptions that occur during arithmetic operations, such as DivideByZeroException and OverflowException. ArrayTypeMismatchExcepti on Thrown when an array cannot store a given element because the actual type of the element is incompatible with the actual type of the array. DivideByZeroExceptionThrown when an attempt is made to divide an integral value by zero. IndexOutOfRangeException Thrown when an attempt is made to index an array when the index is less than zero or outside the bounds of the array. InvalidCastException Thrown when an explicit conversion from a base type to an interface or to a derived type fails at runtime. NullReferenceExceptionThrown when you attempt to reference an object whose value is null. OutOfMemoryException Thrown when an attempt to allocate memory using the new operator fails. This indicates that the memory available to the Common Language Runtime has been exhausted. OverflowExceptionThrown when an arithmetic operation in a checked context overflows. StackOverflowException Thrown when the execution stack is exhausted by having too many pending method calls; usually indicates a very deep or infinite recursion. TypeInitializationException Thrown when a static constructor throws an exception and no compatible catch clause exists to catch it.

Exception Handling Consider the following pseudocode: Perform a task If the preceding task did not execute correctly Perform error processing Perform next task If the preceding task did not execute correctly Perform error processing … In this pseudocode, we begin by performing a task; then we test whether that task executed correctly. If not, we perform error processing. 14-4

Exception Handling int SafeDivision(int x, int y) { try { return (x / y); } catch (System.DivideByZeroException dbz) { System.Console.WriteLine("Division by zero attempted!"); return 0; } 1-5

Exception Handling Exception handling enables programmers to remove error-handling code from the “main line” of the program’s execution. Programmers can decide to handle all exceptions, all exceptions of a certain type or all exceptions of related types. Such flexibility reduces the likelihood that errors will be overlooked. 14-6

try Block A try block encloses code that might throw exceptions and code that is skipped when an exception occurs. try { open file perform work on file close file } 14-7

Catch Block When an exception occurs in a try block, a corresponding catch block catches the exception and handles it. At least one catch block must immediately follow a try block. A catch block specifies an exception parameter representing the exception that the catch block can handle. Optionally, you can include a catch block that does not specify an exception type to catch all exception types. 14-8

Catch Block catch(IO.FileNotFoundException fnfe) { handle file not found (using fnfe object) } catch(Exception e) { handle other type of exception (using e object) close file } 14-9

Exception Handling- Termination Model When a method called in a program or the CLR detects a problem, the method or the CLR throws an exception. The point at which an exception occurs is called the throw point If an exception occurs in a try block, program control immediately transfers to the first catch block matching the type of the thrown exception. After the exception is handled, program control resumes after the last catch block

finally Block Programs frequently request and release resources dynamically. Operating systems typically prevent more than one program from manipulating a file. Therefore, the program should close the file (i.e., release the resource) so other programs can use it. If the file is not closed, a resource leak occurs. The finally block is guaranteed to execute regardless of whether an exception occurs

finally Block Local variables in a try block cannot be accessed in the corresponding finally block, so variables that must be accessed in both should be declared before the try block

Reminder Do not place try blocks around every statement that might throw an exception. It’s better to place one try block around a significant portion of code, and follow this try block with catch blocks that handle each possible exception. Then follow the catch blocks with a single finally block. Separate try blocks should be used when it is important to distinguish between multiple statements that can throw the same exception type

Parallel Computing Parallel computing is a form of computation in which many operations are carried out simultaneously. Many personal computers and workstations have two or four cores which enable them to execute multiple threads simultaneously. Computers in the near future are expected to have significantly more cores

Parallel Computing To take advantage of the hardware of today and tomorrow, software developers can parallelize their code to distribute work across multiple processors. In the past, parallelization required low-level manipulation of threads and locks. 1-15

Task Parallel Library The Task Parallel Library (TPL) is a set of public types and APIs in the System.Threading namespace in the.NET Framework. This relies on a task scheduler that is integrated with the.NET ThreadPool. The purpose of the TPL is to make developers more productive by simplifying the process of adding parallelism and concurrency to applications

Task Parallel Library The TPL scales the degree of concurrency dynamically to most efficiently use all the processors that are available. Parallel code that is based on the TPL not only works on dual-core and quad-core computers. It will also automatically scale, without recompilation, to many-core computers

Task Parallel Library When you use the TPL, writing a multithreaded for loop closely resembles writing a sequential for loop. The following code automatically partitions the work into tasks, based on the number of processors on the computer. 1-18

The Parallel for and foreach loops Parallel.For(startIndex, endIndex, (currentIndex) => DoSomeWork(currentIndex)); // Sequential version foreach (var item in sourceCollection) { Process(item); } // Parallel equivalent Parallel.ForEach(sourceCollection, item => Process(item)); 14-19

Code segments Use.Net Parallel Extensions (Task Parallel Library) and then see the sample/tutorial on how to get this working in the VS IDE. The following example uses lambda expressions, but you can assume this is "magic syntax" for now. You could also use delegates syntax if you'd like.lambda expressionsdelegates 14-20

Code segments You should see increased performance (reduced time to complete) relative to the number of processors/cores you have on the machine. The final example below is using a shared "results" collection, so there is significant blocking and reduced performance. How might we solve this? 14-21