Apr 23-25.exceptions

Apr 23-25.exceptions - Exception Handling in C Topics...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Exception Handling in C++ Topics: • Basics of C++ Exception Handling: try , throw , catch • A Simple Exception-Handling Example: Divide by Zero • Throwing an Exception • Catching an Exception • Rethrowing an Exception • Exception Specifications • Processing Unexpected Exceptions • Stack Unwinding • Constructors, Destructors and Exception Handling • Exceptions and Inheritance
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Basic Concepts and Terms •A n exception is any unusual event, either erroneous or not, detectable by either hardware or software, that may require special processing The special processing that may be required after detection of an exception is called exception handling An exception is raised when its associated event occurs Common failures that raise exceptions new (or gcnew) not allocating memory – out of bounds array subscript – division by zero
Background image of page 2
Ways of handling errors in C++ •U s e assert – prototype for assert: void assert( int expression ); What exactly is assert and what does it do? it’s an ANSI standard function (actually a macro) that evaluates an expression when the result is false, it prints a message and terminates the program How do you use it? you have to include the appropriate header assert.h when checking for an error condition using assert, the expression in the assert function’s argument has to evaluate to nonzero or zero if the expression evaluates to false, you’ll get a message from the system that says the program has terminated in an unusual way – you’ll get a diagnostic message that includes the failed
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Using assert When you write an assert statement, you are saying “this condition must be true or else we have an error” Example – if you are writing a function that expects to see a string pointer as an argument: void ClassX::Func (char* sptr){ if (*sptr == “?”) DoSomething(); } In this code, the function tries to read the memory the pointer points to, so it had better be pointing to something valid, or else you’re going to crash. What if a null pointer got passed? Crash!! Problem is, you don’t have much info about why it crashed – probably some message box that says you tried to read memory at some location and you have no idea what that really means. Rewrite a little: void ClassX::Func (char* sptr){ assert (sptr); if (*sptr == “?”) DoSomething(); } It will still crash, but you’ll get info about which line and which file triggered the assertion
Background image of page 4
#include "stdafx.h" #include <assert.h> #include <string.h> using namespace System; using namespace std; void analyze_string( char *string ); /* Prototype */ i nt main( void ) { char test1[] = "abc", test2[] =""; cout << "Analyzing string " << test1 << "\n"; analyze_string( test1 ); cout << "Analyzing string " << test2 << "\n"; analyze_string( test2 ); } /* Tests a string to see if it is NULL, */
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 31

Apr 23-25.exceptions - Exception Handling in C Topics...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online