void fun() C::C() // ctor try try try : member( ) catch (const E& e) { catch (const E& e) { catch (const E& e) {

Similar documents
CS11 Advanced C++ Fall Lecture 3

Object-Oriented Principles and Practice / C++

Outline. Zoltán Porkoláb: C++11/14 1

! Errors can be dealt with at place error occurs

Assertions and Exceptions

C++ Crash Kurs. Exceptions. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

Lecture Material. Exceptions

A Whirlwind Tour of C++

17.1 Handling Errors in a Program

21. Exceptions. Advanced Concepts: // exceptions #include <iostream> using namespace std;

Introduction. Common examples of exceptions

Exception Handling Pearson Education, Inc. All rights reserved.

CS102 C++ Exception Handling & Namespaces

Throwing exceptions 02/12/2018. Throwing objects. Exceptions

CPSC 427: Object-Oriented Programming

COMP6771 Advanced C++ Programming

IS 0020 Program Design and Software Tools

1 Introduction to C++ C++ basics, simple IO, and error handling

C++ Programming Lecture 5 Software Engineering Group

Contents. Error Handling Strategies (cont d) Error handling strategies: error code, assert(), throw-try-catch

Exception Handling in C++

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Exception Safe Coding

05-01 Discussion Notes

Unit 4 Basic Collections

Laboratorio di Tecnologie dell'informazione

CS 485 Advanced Object Oriented Design. Enum. Spring 2017

Lecture 10. To use try, throw, and catch Constructors and destructors Standard exception hierarchy new failures

Axivion Bauhaus Suite Technical Factsheet AUTOSAR

Intermediate Programming, Spring 2017*

Object-Oriented Programming for Scientific Computing

C++: Const Function Overloading Constructors and Destructors Enumerations Assertions

Object Oriented Programming CS250

CS 162, Lecture 25: Exam II Review. 30 May 2018

Exception Namespaces C Interoperability Templates. More C++ David Chisnall. March 17, 2011

Programming with C++ Exceptions

C++ Namespaces, Exceptions

Chapter 5. Object- Oriented Programming Part I

CPSC 427: Object-Oriented Programming

CSCI 104 Exceptions. Mark Redekopp David Kempe

G52CPP C++ Programming Lecture 16

class Polynomial { public: Polynomial(const string& N = "no name", const vector<int>& C = vector<int>());... };

Programming II (CS300)

C++ Exception Handling. Dr. Md. Humayun Kabir CSE Department, BUET

Practice test for midterm 3 solutions

CS3157: Advanced Programming. Outline

This examination has 11 pages. Check that you have a complete paper.

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University)

C++ Exception Handling 1

TEMPLATES AND EXCEPTION HANDLING

2 ADT Programming User-defined abstract data types

Programming II (CS300)

Advanced C++ Programming Workshop (With C++11, C++14, C++17) & Design Patterns

CS11 Introduction to C++ Fall Lecture 6

Inclusion Polymorphism

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

Variant: a type-safe union without undefined behavior (v2).

Exception Handling Introduction. Error-Prevention Tip 13.1 OBJECTIVES

04-24/26 Discussion Notes

Exceptions, Case Study-Exception handling in C++.

C The new standard

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

CSC 330 Object-Oriented Programming. Exception Handling CSC 330

OBJECT ORIENTED PROGRAMMING USING C++

CPSC 427: Object-Oriented Programming

TABLE OF CONTENTS...2 INTRODUCTION...3

COMP322 - Introduction to C++ Lecture 10 - Overloading Operators and Exceptions

School of Informatics, University of Edinburgh

Traditional Error Handling

Instantiation of Template class

Welcome to Teach Yourself Acknowledgments Fundamental C++ Programming p. 2 An Introduction to C++ p. 4 A Brief History of C++ p.

C++ Programming: Polymorphism

Homework 4. Any questions?

Absolute C++ Walter Savitch

Mandating the Standard Library: Clause 18 - Diagnostics library

VALLIAMMAI ENGINEERING COLLEGE

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

G Programming Languages - Fall 2012

Object-Oriented Programming

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Extending Classes (contd.) (Chapter 15) Questions:

C++ TEMPLATES. Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type.

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

Operators. The Arrow Operator. The sizeof Operator

Short Notes of CS201

Error Handling in C++

Internationalization and Error Handling

Exception Handling. Sometimes when the computer tries to execute a statement something goes wrong:

Engineering Robust Server Software

CS201 - Introduction to Programming Glossary By

Qualified std::function signatures

STRUCTURING OF PROGRAM

Lecture 8. Exceptions, Constructor, Templates TDDD86: DALP. Content. Contents Exceptions

Exception Handling. Run-time Errors. Methods Failure. Sometimes when the computer tries to execute a statement something goes wrong:

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management

Transcription:

TDDD38 APiC++ Exception Handling 134 Exception handling provides a way to transfer control and information from a point in the execution to an exception handler a handler can be invoked by a throw expression in the handler s try block, or thrown in a function called from the try block try block function try block exception specification throw expression handler exception declaration void fun() C::C() // ctor try try try : member( ) { { { fun(); if (disaster) throw E{; catch (const E& e) { catch (const E& e) { catch (const E& e) { throw; // rethrows e // e re-thrown implicitly! a throw expression has type void initializes a temporary object the exception object in a handler, a simple throw expression rethrows the caught exception object in a function try block of a constructor or a destructor the caught exception object is always implicitly rethrown

TDDD38 APiC++ Exception Handling 135 Handling an exception handlers of a try block are tried in order of appearance makes it possible to write handlers that can never be executed: try { catch (const exception& e) { catch (const logic_error& e) { // catches also subtype logic_error // dead code if no match is found among the handlers of at try block, the search continues in a dynamically surrounding try block if no handler is found, the program calls terminate() which in turn calls abort()... (ellipsis) in a handler s exception declaration specifies a match for any exception must be the last handler for a try block, if used catch (...) { // catch-all handler an exception is considered handled upon entry to a handler the stack will be unwound at that point

TDDD38 APiC++ Exception Handling 136 Exception specifications Two forms, one new and one deprecated. noexcept specification void fun() noexcept(false); void fun() noexcept(true); void fun() noexcept; // equivalent to noexcept(true) the expression supplied to noexcept shall be a constant expression convertible to bool if a noexcept(true) specification is violated the program will call std::terminate() dynamic exception specification deprecated in C++11 don t use dynamic exception specifications are handled dynamically no static checks code size may increase void fun() throw (range_error, length_error); void fun() throw (); // does not throw if violated the program will call std::terminate() void fun() throw (range_error, length_error, bad_exception); any exception not in the exception specification will be replaced with std::bad_exception

TDDD38 APiC++ Exception Handling 137 Special functions used by the exception handling mechanism [[noreturn]] void terminate() noexcept; called when exception handling must be abandoned for other error handling techniques; calls a terminate handler function the default terminate handler function can be replaced by calling set_terminate(my_terminate_handler) the attribute [[noreturn]] specifies that a function does not return [[noreturn]] void abort() noexcept; is not directly associated with exception handling but called by the default terminate handler function. terminates the program without executing destructors for object of automatic, thread or static storage duration There is more

TDDD38 APiC++ Exception Handling 138 Standard exceptions exception logic_error domain_error invalid_argument length_error out_of_range future_error runtime_error range_error overflow_error underflow_error system_error ios_base::failure bad_typeid bad_cast bad_weak_pointer bad_exception bad_function_call bad_alloc bad_array_new_length e.g thrown by/when: illegal functions values bitset constructor object length is exceeded at() functions in the thread library certain computations bitset::to_long() certain computations system related functions ios_base::clear() typeid dynamic_cast std::shared_ptr constructors exception specification std::function::operator() new new[]

TDDD38 APiC++ Exception Handling 139 Class exception Base class for all standard exceptions. class exception { public: exception() noexcept; exception(const exception&) noexcept; virtual ~exception() noexcept; exception& operator=(const exception&) noexcept; ; virtual const char* what() const noexcept; what() returns an implementation-dependent message subclasses carry their own messages, supplied when thrown

TDDD38 APiC++ Exception Handling 140 Class logic_error Example of a typical direct subclass to exception. class logic_error : public exception { public: explicit logic_error(const string& what_arg) noexcept : msg_{ what_arg { explicit logic_error(const char* what_arg) noexcept : msg_{ what_arg { virtual const char* what() const noexcept { return str_.data(); private: string ; msg_; copy constructor and copy assignment operator are compiler generated logic_error, runtime_error, and their subclasses are suitable to derive your own exception classes from

TDDD38 APiC++ Exception Handling 141 Class length_error Example of a typical second level subclass to exception a concrete exception class. class length_error : public logic_error { public: explicit length_error(const string& what_arg) noexcept : logic_error{ what_arg { ; explicit length_error(const char* what_arg) noexcept : logic_error{ what_arg { all functionality is inherited from logic_error, only two constructors need to be defined the standard exception hierarchy can easily be extended with user defined exceptions like length_error and alike

TDDD38 APiC++ Exception Handling 142 Streams and exceptions When an I/O failure occur, a stream by default silently freezes. the stream position locks on the faulty position the operation fails returns false if it s a bool returning operation all following reads will also fail the occurrence of the failure may not be obvious As an alternative, a stream can be set to throw an exception, e.g. cin.exceptions(ios::eofbit); clog.exceptions(ios::badbit ios::failbit); The exception thrown will be ios::failure, a subtype to exception::runtime_error::system_error try { cin >> x; catch (const ios::failure& e) { cout << e.what() << \n ; // will tell about the cause to the read failure

TDDD38 APiC++ Exception Handling 143 Runtime assertions #include <cassert> void print(int* p) { assert(p!= nullptr); cout << *p; if the expression is false, a message is printed and abort() is called the message shall include the expression whose assertion failed, the name of the source file, and the line number where it happened, usually Assertion failed: expression, file filename, line line number assert is designed to capture programming errors, not user or running errors generally disabled after a program exits its debugging phase assertion checks are disabled if the macro NDEBUG is defined when <cassert> is included #define NDEBUG 1 #include <cassert> g++ -DNDEBUG

TDDD38 APiC++ Exception Handling 144 Static assertions static_assert(sizeof(long) >= 8, "64-bit code generation required"); a constant expression that can be contextually converted to bool, and a string literal if the expression is true the declaration has no effect if the expression is false the resulting diagnostic message shall include the string literal type traits for inquiring about type properties and type relations is a new interesting possibility in C++11 static_assert(std::numeric_limits<t>::is_integer, "T must be an integer!"); T is supposed to be a template type parameter static_assert(std::is_same<std::result_of(fun())>::type, short, "Error!");

TDDD38 APiC++ Exception Handling 145 Recommendations for error handling and exceptions (1) design and write error-safe code give the strongest safety guarantee which is reasonable in each case The Basic Guarantee: ensure that errors always leave your program in a valid state The Strong Guarantee: prefer to additionally guarantee that the final state is either the original state or the intended target state The No-Fail Guarantee: prefer to additionally guarantee that the operation can never fail prefer to use exceptions over error codes to report errors exceptions can t be silently ignored exceptions propagate automatically exception handling removes error handling and recovery from the main line of control exception handling is better than the alternatives to report errors from constructors and destructors use assertions to document assumptions internal to a module don t use runtime assertions to report run-time errors

TDDD38 APiC++ Exception Handling 146 Recommendations for error handling and exceptions (2) be careful with exception specifications when violated they terminate your program can causes the compiler to inject additional run-time overhead in the form of implicit try/catch blocks to enforce via run-time checking that a function only emit listed exceptions in general one can not write useful exception specifications for template functions writing exception specifications for virtual functions forces overridings to have compatible specifications throw by value catch by reference don t throw by pointer (copying Java syntax) catch by reference (usually to const) to avoid copying and destruction catch by reference to preserve polymorphism when rethrowing an exception E, prefer throw instead of throw E exceptions are well suited for communicating errors between independently developed program parts e.g. library components should report errors by throwing exceptions use other error handling techniques when appropriate, e.g. for dealing with local errors