answersLogoWhite

0


Best Answer

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

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a program to demonstrate the catching of all exceptions what happens when a raised exception is not caught by catch block in the absence of a catch all exception block?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

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.


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)


How do you make an experiment to show that air occupies space?

The easiest way to demonstrate the volume of air is with a balloon. When it is inflated, the air inside is occupying space. Deflate the balloon - it shrinks in the absence of air.


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?

The word &quot;absence&quot; is a noun.


Is their a word for absence of air?

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


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 absence of sound called?

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


What is the meaning of absence?

Absence typically refers to the state of being away or not present. It can also describe a lack or deficiency of something, such as absence of mind (distraction) or absence of evidence.