04a_cpp_except

04a_cpp_except - 1 Data Structures - CSCI 102 Copyright...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 Data Structures - CSCI 102 Copyright William C. Cheng CS102 C++ Exception Handling & Namespaces Bill Cheng http://merlot.usc.edu/cs102-s11 Polymorphism (Ch 13) Virtual functions Abstract classes Interfaces Pointers & Dynamic Objects (Ch 13) C Structs (Ch 10) Topics to cover 2 Data Structures - CSCI 102 Copyright William C. Cheng C++ Object-Oriented Programming C++ Classes (Ch 11) Constructors Destructors Member functions Exception Handling (Ch 15) Namespaces (Ch 8) Operator Overloading (Ch 14) Class Composition & Inheritance (Ch 12) Return a special value from the function? When something goes wrong in one of your functions, how should you notify to the function caller? 3 Data Structures - CSCI 102 Copyright William C. Cheng Exception Handling Return a bool indicating success/failure? Set a global variable? Print out an error message? Print an error and exit the program? Set a failure flag on somewhere (like " cin " does)? Handle the problem and just dont tell the caller? You should always notify the caller something happened. Silence is not an option. Theres something wrong with all those options... 4 Data Structures - CSCI 102 Copyright William C. Cheng Exception Handling You cant always return an error state (what if a function is supposed to return " bool "?) What if something goes wrong in a Constructor? What if the function where the error happens isnt equipped to handle the error? All these strategies are passive . They require the caller to actively check if something went wrong. You dont have a return value available Good sanity checks for development/testing The assert statement allows you to make sure certain conditions are true and immediately halt your program if theyre not 5 Data Structures - CSCI 102 Copyright William C. Cheng The " assert " Statement Not ideal for an end product #include <cassert> int divide( int num, int denom) { assert(denom != ); return (num/denom); } throw interrupts the normal flow of execution Used when code has encountered a problem, but the current code cant handle that problem itself 6 Data Structures - CSCI 102 Copyright William C. Cheng The " throw " Statement #include <cassert> int divide( int num, int denom) { if(denom == ) throw denom; return (num/denom); } Gives the caller the opportunity to catch and handle it If nothing deals with it, the program will terminate What can you give to the throw statement? Anything! But some things are better than others... The value that is thrown may not always be meaningful Dont throw primitive values (e.g. an " int ") 7 Data Structures - CSCI 102 Copyright William C. Cheng What Should You " throw "? throw 123 ; Provides no other context (what happened & where?) Works for a human, but not much help to an application Dont throw " string " throw "Someone passed in a 0 and stuff broke!" ; Serves as the basis for building your own exceptions Use the <stdexcept> header file throw std::invalid_argument( " Denominator cant be 0!"Denominator cant be 0!...
View Full Document

Page1 / 29

04a_cpp_except - 1 Data Structures - CSCI 102 Copyright...

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

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