Uncaught Exceptions from a function - C++ Statement

C++ examples for Statement:try catch

Introduction

A function with its own try block does not have to catch every possible error generated within the try.

Some exceptions can slip through to, and be handled in, outer scopes.

Demo Code


#include <iostream>
#include <stdexcept>

void function() {
    try {//from  w ww . ja v a2  s . c om
        if (false)
            throw 9;  // caught in function
        else
            throw 10.5;  // caught in main
    } catch (int e) {
        std::cout << "Caught in Function: i: " << e << std::endl;
    }
}

int main(int argc, const char *argv[]) {
    try {
        function();
    } catch (double e) {
        std::cout << "Caught in main: d: " << e << std::endl;
    } catch (...) {
        std::cout << "test" << std::endl;
    }
    return 0;
}

Result


Related Tutorials