answersLogoWhite

0

If an exception is not caught then your program has undefined behaviour. Ultimately the program crashes, but since you haven't handled the exception you've no way of knowing what damage has been done. Files could be wiped or currupted, planes could fall from the sky... anything is possible with undefined behavour.

The best way to deal with unknown exceptions is to first catch them with a catch-all. However, you cannot actually handle the exception unless you know what type of exception you are actually dealing with. Thus the normal course of action is to assume the worst, perform any and all necessary cleanup, log the exception as an unknown exception and rethrow. As the exception percolates back down the call stack, all other exception handlers should do the same: cleanup, log and rethrow. If you're lucky, another handler might recognise the exception and be able to provide more detailed information on the type of exception. Ultimately you must never allow a program to continue executing if you cannot handle an exception.

C++11 offers a more elegant solution using a nested try catch within a catch-all. However, it makes more sense to place the nested try catch in a global function (e.g., handle_eptr()) which can specifically deal with all catch-all exceptions. Like so...

#include <iostream>

#include <string>

#include <exception>

#include <stdexcept>

void handle_eptr(std::exception_ptr eptr)

{

try

{

if (eptr) std::rethrow_exception (eptr);

}

catch (const std::exception& e)

{

std::cerr << "Exception: "" << e.what() << ""\n";

}

}

int main()

{

try

{

std::string().at(1); // throws "invalid string position"

}

catch(...) // catch-all

{

// perform any necessary cleanup here before calling the global handler

handle_eptr (std::current_exception());

}

}

Output:

Exception: "invalid string position"

Note that handle_eptr() receives a std::exception_ptr. By itself this is useless, but std::rethrow_exception() converts the eptr to a standard exception which can then be thrown, caught and logged. You must still perform any necessary cleanup and allow the program to terminate gracefully, but at least you now know what type of exception you are dealing with and can provide a specific handler to handle it.

Note that handle_eptr() should be fleshed out to accept all necessary debug information such as the filename and the line number from the catch_all that called it. The example merely demonstrates how to pass the eptr and convert it into an actual exception.

User Avatar

Wiki User

11y ago

What else can I help you with?

Related Questions

Do both hot and cold have energy?

Yes (with the exception of absolute zero) but the energy in hot and cold is always heat energy. (Cold is defined as the absence of heat)


what happens in the absence of a chromosome?

Generally, absence of a chromosome results in EED (early embryonic death). The exception is the absence of one of the sex chromosomes. While a Child born with a single X chromosome has some physiologic deficits they can survive into adulthood.


What is rainbow colours calle?

Every color that a human eye has ever perceived and given a name is in the rainbow. (With the exception of 'black', which is a name for the absence of any color, and 'white', which is the superposed presence of all colors.)


What is death as defined by the American Medical Association?

The absence of brain activity.The absence of brain activity.The absence of brain activity.The absence of brain activity.The absence of brain activity.The absence of brain activity.


What is the absence of nothing?

The absence of nothing is everything. The absence of nothing is everything.


What part of speech is the word absence?

Absence is a noun.


What is the absence of sound called?

The absence of sound is called silence.Quiet or quietness is also the absence of sound.


Is their a word for absence of air?

the absence of air is &quot;vacuum&quot;


Why testing only shows the presence of errors not their absence?

Testing can demonstrate that a system contains errors when failures occur, but it cannot prove that a system is free of errors. This is because testing is inherently limited by factors such as the finite number of test cases and scenarios that can be executed. Even exhaustive testing can only provide confidence in the absence of errors within the tested scope, but cannot account for untested conditions or potential edge cases. Therefore, while testing identifies issues, it cannot guarantee the complete absence of errors.


When was The Absence created?

The Absence was created in 2002.


What is the plural of absence?

The plural of absence is absences.


What is the meaning of absence?

Absence is simply defined as "Not being there."

Trending Questions
Why does the floor drain bubble when the washer drains? What is the virtual function and friend function? The operational effectiveness and operational suitability questions that must be addressed during Operational Test and Evaluation (OT and ampE) to assess the system's capability to perform its mission? Where is the condenser motor located in your home ac? What are the steps to convert a IEEE 754 binary floating point representation single precision to decimal? What is the starting salary for a Chemical Engineer in South Africa? What inventions did Andrew Carnegie make? Three bulbs of different watts are connected in series - which bulb will burn brightly - the higher wattage bulb or the lower? What are the 3 characteristics that categorize integrated circuits? Why is a handle made of metal? What is the difference between algorithm and pseudocode in computer science? How long does a programmer take to make a professional Program? A workshop designed to retrain workers 55 years of age and older who have lost their jobs is proposed. Suppose the workshop will increase the income of each participant by 1000 per year for a period o? How does a power inverter work? What are nanopolymers? What is the meaning of UB in structural engineering? What is the analogy of drill to bore? How does electromagnetic induction produce induced current? What size wire and breaker for 125hp 240 volts 3 phase motor? What is the difference between h orizontal and vertical organizational structure?