1 JMH Associates © 2004, All rights reserved Chapter 4 Structured Exception Handling.

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

Topics Introduction Types of Errors Exceptions Exception Handling
More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
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.
An Introduction to Java Programming and Object- Oriented Application Development Chapter 8 Exceptions and Assertions.
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.
 Both System.out and System.err are streams—a sequence of bytes.  System.out (the standard output stream) displays output  System.err (the standard.
Exception Handling 1 CISC6795, Spring Introduction 2 Exception – an indication of a problem that occurs during a program’s execution, for examples:
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Exceptions Briana B. Morrison CSE 1302C Spring 2010.
Introduction to Programming Lesson 1. Objectives Skills/ConceptsMTA Exam Objectives Understanding Computer Programming Understand computer storage and.
Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling.
Exception Handling Introduction Exception handling is a mechanism to handle exceptions. Exceptions are error like situations. It is difficult to decide.
CS Advanced C++ Exception Handling Topic #5.
Chapter 10.
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.
Finding and Debugging Errors
Java Review 2 – Errors, Exceptions, Debugging Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Exceptions COMPSCI 105 S Principles of Computer Science.
June 14, 2001Exception Handling in Java1 Richard S. Huntrods June 14, 2001 University of Calgary.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
Assertions Program correctness. Assertions Java statement – enables you to assert an assumption about your program. – An assertion contains a Boolean.
Object Oriented Programming
Win32 Programming Lesson 24: More SEH That’s right… you’ll never generate an exception, will you?
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
WEEK EXCEPTION HANDLING. Syntax Errors Syntax errors, also known as parsing errors, are perhaps the most common kind of complaint you get while.
COMPUTER PROGRAMMING 2 Exceptions. What are Exceptions? Unexpected events that happen when the code is executing (during runtime). Exceptions are types.
Tutorial 11 Using and Writing Visual Basic for Applications Code
The Java Programming Language
Exceptions. Exception Abnormal event occurring during program execution Examples –Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
VB.Net - Exceptions Copyright © Martin Schray
Introduction to Exception Handling and Defensive Programming.
Chapter 24 Exception CSC1310 Fall Exceptions Exceptions Exceptions are events that can modify the flow or control through a program. They are automatically.
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.
Controlling Execution Programming Right from the Start with Visual Basic.NET 1/e 8.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
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.
Module 6: Debugging a Windows CE Image.  Overview Debug Zones IDE Debug Setup IDE Debug Commands Platform Builder Integrated Kernel Debugger Other Debugging.
Writing a Run Time DLL The application loads the DLL using LoadLibrary() or LoadLibraryEx(). The standard search sequence is used by the operating system.
Interrupt driven I/O. MIPS RISC Exception Mechanism The processor operates in The processor operates in user mode user mode kernel mode kernel mode Access.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Exceptions Chapter 16 This chapter explains: What as exception is Why they are useful Java exception facilities.
Exceptions and Assertions Chapter 15 – CSCI 1302.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Exceptions. Exception  Abnormal event occurring during program execution  Examples Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
Interrupt driven I/O Computer Organization and Assembly Language: Module 12.
Error Handling Tonga Institute of Higher Education.
Firoze Abdur Rakib. Syntax errors, also known as parsing errors, are perhaps the most common kind of error you encounter while you are still learning.
Exception Handling How to handle the runtime errors.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Interrupts and Exception Handling. Execution We are quite aware of the Fetch, Execute process of the control unit of the CPU –Fetch and instruction as.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
1 Handling Errors and Exceptions Chapter 6. 2 Objectives You will be able to: 1. Use the try, catch, and finally statements to handle exceptions. 2. Raise.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Agenda Introduction Errors and Exception Exception Hierarchy Classification of Exceptions Built in Exceptions Exception Handling in Java User defined.
Eighth Lecture Exception Handling in Java
CSCE Student presentation LARRY PARKER
Crash Handlers Riddhiman Ghosh Debugging Applications for
Topics Introduction to File Input and Output
Programming in C# CHAPTER - 7
Topics Introduction to File Input and Output
Crash Handlers Riddhiman Ghosh Debugging Applications for
Presentation transcript:

1 JMH Associates © 2004, All rights reserved Chapter 4 Structured Exception Handling

2 JMH Associates © 2004, All rights reserved OBJECTIVESOBJECTIVES Upon completion of this chapter, you will be able to:  Describe Windows and Visual C++ Structured Exception Handling (SEH)  Use SEH to detect and analyze exceptions  Both user- and system-generated  Simplify your code’s response to exceptions and errors  Using exception and termination handlers  Protect your code from unexpected exceptions  And properly clean up and terminate your program

3 JMH Associates © 2004, All rights reserved OVERVIEWOVERVIEW Structured Exception Handling (SEH) provides a robust mechanism for applications to respond to unexpected events  Hardware faults  Addressing exceptions  System errors  User generated exceptions SEH assures the ability to free resources  Perform clean-up SEH simplifies program logic

4 JMH Associates © 2004, All rights reserved PARTSPARTS Part IException Handlers Part IITermination Handlers Lab 4–A

5 JMH Associates © 2004, All rights reserved PART I Exception Handlers

6 JMH Associates © 2004, All rights reserved TRY AND EXCEPT BLOCKS __try and __except  Keywords recognized by Visual C++ compiler  Actual keywords are specific to the compiler __try { /* Block of monitored code */ } __except (filter_expression) { /* Exception handling block */ }

7 JMH Associates © 2004, All rights reserved SEH, BLOCKS, AND FUNCTIONS { DWORD x1; /* Block 1 */... _try { /* Block 2 */ DWORD x2;.... x2 = f (x1);.... } _except () { /* SEH 2 */ } DWORD f (DWORD y) { /* Block f */ DWORD z; z = y / (y - 1); return z / y; } STACK Windows Exception Handler Block 1 x1 Block 2 x2 SEH 2 Block f y z Exception Occurs Execute this SEH

8 JMH Associates © 2004, All rights reserved FILTER EXPRESSIONS The filter_expression is evaluated after an exception It is a literal constant, expression, or a function call Returns one of three values  EXCEPTION_EXECUTE_HANDLER  Normal case  EXCEPTION_CONTINUE_SEARCH  This handler does not process this exception; unwind the stack for the next exception handler  EXCEPTION_CONTINUE_EXECUTION  Some exceptions cannot be continued, and another exception would occur immediately

9 JMH Associates © 2004, All rights reserved EXCEPTION CODES (1 of 2) How do you know what exception occurred?  And how do you know what filter expression value to generate? DWORD GetExceptionCode (VOID)  The filter function itself cannot call GetExceptionCode, so a common usage is: __except (MyFilter (GetExceptionCode ())) {... }

10 JMH Associates © 2004, All rights reserved EXCEPTION CODES (2 of 2) An alternative function that returns additional information: LPEXCEPTION_POINTERS GetExceptionInformation (VOID) including information on the virtual address causing an access violation

11 JMH Associates © 2004, All rights reserved EXCEPTION CODE VALUES (1 of 2) 1.Program violations, including:  EXCEPTION_ACCESS_VIOLATION  EXCEPTION_DATATYPE_MISALIGNMENT  EXCEPTION_NONCONTINUABLE_EXECUTION 2..Memory allocation exceptions (See Chapter 4) ( HeapAlloc and HeapCreate )  STATUS_NO_MEMORY

12 JMH Associates © 2004, All rights reserved EXCEPTION CODE VALUES (2 of 2) 3.User-defined exception codes 4.Arithmetic codes, such as:  EXCEPTION_INT_DIVIDE_BY_ZERO  EXCEPTION_FLT_DIVIDE_BY_ZERO 5.Debugger exceptions — EXCEPTION_BREAKPOINT

13 JMH Associates © 2004, All rights reserved EXCEPTION CONTROL FLOW _try {... i = j / 0;... } _except (Filter (GetExceptionCode ())) {... }... DWORD Filter (DWORD ExCode) { switch (ExCode) {... case EXCEPTION_INT_DIVIDE_BY_ZERO:... return EXCEPTION_EXECUTE_HANDLER; case... }

14 JMH Associates © 2004, All rights reserved USER-GENERATED EXCEPTIONS (1 of 2) VOID RaiseException (DWORD dwExceptionCode, DWORD dwExceptionFlags, DWORD cArguments, LPDWORD lpArguments) dwExceptionCode bits 31, 30: 0 — Success1 — Informational 2 — Warning3 — Error Bit 29: Set Bit 28: 0Bits 27–0: User Specified Typical value: 0XE

15 JMH Associates © 2004, All rights reserved USER-GENERATED EXCEPTIONS (2 of 2) dwExceptionFlags — EXCEPTION_NONCONTINUABLE indicates filter expression should not generate EXCEPTION_CONTINUE_EXECUTION lpArguments — If not NULL, points to an array of cArguments EXCEPTION_MAXIMUM_PARAMETERS == 15

16 JMH Associates © 2004, All rights reserved ReportException Function (1 of 2) VOID ReportException (LPCTSTR UserMessage, DWORD ExceptionCode) /* ReportException */ /* Extension of ReportError to generate a user exception code rather than terminating. */ /* UserMessage: Message to be displayed ExceptionCode: 0 - Return > 0 - ExitProcess with this code */ { /* Report as a non-fatal error */ if (lstrlen (UserMessage) > 0) ReportError (UserMessage, 0, TRUE);

17 JMH Associates © 2004, All rights reserved ReportException Function (2 of 2) /* If fatal, raise an exception */ /* Mask out any high order bits in the */ /* user-supplied exception code */ if (ExceptionCode != 0) RaiseException ( (0x0FFFFFFF & ExceptionCode) | 0xE , 0, 0, NULL); return; }

18 JMH Associates © 2004, All rights reserved ENABLING FLOATING POINT EXCEPTIONS (1 of 2) Skip if you are not interested in floating point arithmetic To enable floating point exceptions, you need to set the fp mask (set the bit off to enable the exception) int iFPMask; /* Save old control mask */ iFPMask = _controlfp (0, 0); iFPMask &= ~(EM_OVERFLOW | EM_UNDERFLOW | EM_INEXACT | EM_ZERODIVIDE | EM_DENORMAL); _controlfp (iFPMask, MCW_EM); /* Set new control mask*/ /* Restore the old mask to disable exceptions */ _controlfp (iFPMask, 0xFFFFFFFF);

19 JMH Associates © 2004, All rights reserved ENABLING FLOATING POINT EXCEPTIONS (2 of 2) Formula: (current_mask & ~mask) | (new & mask) After a FP exception, clear it with _clearfp()

20 JMH Associates © 2004, All rights reserved PART II Termination Handlers

21 JMH Associates © 2004, All rights reserved TRY-FINALLY BLOCKS Example: The finally block (almost) always exectutes while (...) __try { hTemp = CreateFile (TempFileName,...); /* Block of guarded code */... if (...) break; /* finally block will run */... } __finally { /* Executes on every loop iteration */ /* termination handler (finally block) */ CloseHandle (hTemp); DeleteFile (TempFileName); }

22 JMH Associates © 2004, All rights reserved TERMINATION HANDLERS Executed whenever control flow leaves the try block because of:  Reaching the end of the __try block  Leaving the block because of execution of: returnbreaklongjump continuegoto__leave  An exception Executed in the context of the block or function it guards Control can pass from end of termination handler to next statement

23 JMH Associates © 2004, All rights reserved ABNORMAL TERMINATION To detect termination for any reason other than reaching the end of the try block—use BOOL AbnormalTermination (VOID) to determine how the try block terminated TRUE — For abnormal termination FALSE — For normal termination

24 JMH Associates © 2004, All rights reserved COMBINING FINALLY AND EXCEPT BLOCKS (1 of 2) __try { /* Block of guarded code */ } __except (filter_expression) { /* Except block */ } __finally { /* You cannot do this ! */ }

25 JMH Associates © 2004, All rights reserved COMBINING FINALLY AND EXCEPT BLOCKS (2 of 2) __try { /* Outer try-except block */ while (...) __try { /* Inner try-fin block */ hFile = CreateFile (TempFile,...); if (...) __try { /* Inner try-ex block */... } __except (EXCEPTION_EXECUTE_HANDLER) {... /* Process FP exception. */ _clearfp(); } __finally { /* End of while loop */ CloseHandle (hFile); DeleteFile (TempFile); } __except (filter-expression) { }

26 JMH Associates © 2004, All rights reserved LAB 4–A Write a program, toupper, which processes one or more files on the command line  The processing consists of converting lower case characters to upper case and writing the results to a new file  If an input file is missing, ignore it and process the next  Process the files in memory (allocate enough memory to read the entire file)  Use exception handlers to detect and respond to all errors that occur during the processing