MANIFESTACIONES DE JMH AL INFORME PRELIMINAR GUARDERÍA ABC 19032010 REVISADA por JMH
1 JMH Associates © 2004, All rights reserved Chapter 4 Structured Exception Handling.
-
date post
21-Dec-2015 -
Category
Documents
-
view
220 -
download
2
Transcript of 1 JMH Associates © 2004, All rights reserved Chapter 4 Structured Exception Handling.
1JMH Associates © 2004, All rights reserved
Chapter 4Chapter 4Chapter 4Chapter 4
Structured Exception Handling
2JMH Associates © 2004, All rights reserved
OBJECTIVESOBJECTIVESOBJECTIVESOBJECTIVES
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
3JMH Associates © 2004, All rights reserved
OVERVIEWOVERVIEWOVERVIEWOVERVIEW
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
4JMH Associates © 2004, All rights reserved
PARTSPARTSPARTSPARTS
Part I Exception Handlers
Part II Termination Handlers
Lab 4–A
5JMH Associates © 2004, All rights reserved
PART IPART IPART IPART I
Exception Handlers
6JMH Associates © 2004, All rights reserved
TRY AND EXCEPT BLOCKSTRY AND EXCEPT BLOCKSTRY AND EXCEPT BLOCKSTRY 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 */}
7JMH Associates © 2004, All rights reserved
SEH, BLOCKS, AND FUNCTIONSSEH, BLOCKS, AND FUNCTIONSSEH, BLOCKS, AND FUNCTIONSSEH, 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 2x2
SEH 2
Block fyz
Exception OccursExecute this SEH
8JMH Associates © 2004, All rights reserved
FILTER EXPRESSIONSFILTER EXPRESSIONSFILTER EXPRESSIONSFILTER 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
9JMH Associates © 2004, All rights reserved
EXCEPTION CODES (1 of 2)EXCEPTION CODES (1 of 2)EXCEPTION CODES (1 of 2)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 ())) { . . .
}
10JMH Associates © 2004, All rights reserved
EXCEPTION CODES (2 of 2)EXCEPTION CODES (2 of 2)EXCEPTION CODES (2 of 2)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
11JMH Associates © 2004, All rights reserved
EXCEPTION CODE VALUES (1 of 2)EXCEPTION CODE VALUES (1 of 2)EXCEPTION CODE VALUES (1 of 2)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
12JMH Associates © 2004, All rights reserved
EXCEPTION CODE VALUES (2 of 2)EXCEPTION CODE VALUES (2 of 2)EXCEPTION CODE VALUES (2 of 2)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
13JMH Associates © 2004, All rights reserved
EXCEPTION CONTROL FLOWEXCEPTION CONTROL FLOWEXCEPTION CONTROL FLOWEXCEPTION 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 . . .}
}
2
6
7
3
5
4
1
14JMH Associates © 2004, All rights reserved
USER-GENERATED EXCEPTIONSUSER-GENERATED EXCEPTIONS(1 of 2)(1 of 2)
USER-GENERATED EXCEPTIONSUSER-GENERATED EXCEPTIONS(1 of 2)(1 of 2)
VOID RaiseException (DWORD dwExceptionCode,DWORD dwExceptionFlags, DWORD cArguments,LPDWORD lpArguments)
dwExceptionCode bits 31, 30:
0 — Success 1 — Informational
2 — Warning 3 — Error
Bit 29: Set
Bit 28: 0 Bits 27–0: User Specified
Typical value: 0XE0000006
15JMH Associates © 2004, All rights reserved
USER-GENERATED EXCEPTIONSUSER-GENERATED EXCEPTIONS(2 of 2)(2 of 2)
USER-GENERATED EXCEPTIONSUSER-GENERATED EXCEPTIONS(2 of 2)(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
16JMH Associates © 2004, All rights reserved
ReportExceptionReportException Function (1 of 2) Function (1 of 2)ReportExceptionReportException Function (1 of 2) 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);
17JMH Associates © 2004, All rights reserved
ReportExceptionReportException Function (2 of 2) Function (2 of 2)ReportExceptionReportException Function (2 of 2) 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) | 0xE0000000, 0, 0, NULL); return;}
18JMH Associates © 2004, All rights reserved
ENABLING FLOATING POINT ENABLING FLOATING POINT EXCEPTIONS (1 of 2)EXCEPTIONS (1 of 2)
ENABLING FLOATING POINT ENABLING FLOATING POINT EXCEPTIONS (1 of 2)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);
19JMH Associates © 2004, All rights reserved
ENABLING FLOATING POINT ENABLING FLOATING POINT EXCEPTIONS (2 of 2)EXCEPTIONS (2 of 2)
ENABLING FLOATING POINT ENABLING FLOATING POINT EXCEPTIONS (2 of 2)EXCEPTIONS (2 of 2)
Formula: (current_mask & ~mask) | (new & mask)
After a FP exception, clear it with _clearfp()
20JMH Associates © 2004, All rights reserved
PART IIPART IIPART IIPART II
Termination Handlers
21JMH Associates © 2004, All rights reserved
TRY-FINALLY BLOCKSTRY-FINALLY BLOCKSTRY-FINALLY BLOCKSTRY-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);}
22JMH Associates © 2004, All rights reserved
TERMINATION HANDLERSTERMINATION HANDLERSTERMINATION HANDLERSTERMINATION 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:
return breaklongjump
continue goto __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
23JMH Associates © 2004, All rights reserved
ABNORMAL TERMINATIONABNORMAL TERMINATIONABNORMAL TERMINATIONABNORMAL 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
24JMH Associates © 2004, All rights reserved
COMBINING FINALLY ANDCOMBINING FINALLY ANDEXCEPT BLOCKS (1 of 2)EXCEPT BLOCKS (1 of 2)
COMBINING FINALLY ANDCOMBINING FINALLY ANDEXCEPT BLOCKS (1 of 2)EXCEPT BLOCKS (1 of 2)
__try {
/* Block of guarded code */
}
__except (filter_expression) {
/* Except block */
}
__finally {
/* You cannot do this ! */
}
25JMH Associates © 2004, All rights reserved
COMBINING FINALLY ANDCOMBINING FINALLY ANDEXCEPT BLOCKS (2 of 2)EXCEPT BLOCKS (2 of 2)
COMBINING FINALLY ANDCOMBINING FINALLY ANDEXCEPT BLOCKS (2 of 2)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) { }
26JMH Associates © 2004, All rights reserved
LAB LAB 44–A–ALAB LAB 44–A–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