Exception Handling Flashcards

1
Q

What happens if there is local dynamic memory inside the function that throws an error? How do you fix it?

A

At the end of the try block, the memory will be released and will cause a memory leak.
Don’t do exception handling with local dynamic memory or don’t have a try block at the calling routine of the function that manages local dynamic memory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What happens when you have a local class object inside of a function which has a try block at the calling routine and throw takes place?

A

At the end of the try block in the calling routine, the object’s destructor is invoked.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

With exception handling, how does unwinding the stack occur?

A

Don’t have the function that throws the error manage the exception handling. Instead, have the function call inside a try block elsewhere in the program. If the function detects an error. Then the error is thrown back to the calling routine at the end of the try block, then it looks for the matching catch type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What’s the best way to manage exception handling (assuming there’s no local dynamic memory)?

A

To have the exception handling at the calling routine so the function(s) do not handle errors themselves.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What happens if an error is thrown inside nested try blocks?

A

If an error is detected inside nested try blocks it will go the the end of each try block in the next outward scope until it finds a matching catch. If the last try block is reached without finding a matching catch block, then the function terminate will be called and the program will abort.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Can you detect multiple errors within a try block? How?

A

Yes, multiple throws can be detected of either the same or different types.

A catch_all( (..) ) can be used or multiple different types can be used. Multiple catches can also occur following a try block with multiple throws.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How are catch blocks detected after a try block?

A

The try block turns on the exception handler and if a throw occurs inside the try block, then the throw type will find the FIRST matching catch type either specific or a catch_all.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What happens if no error took place in a try block?

A

The catch block is skipped.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you “turn on” the exception handler?

A

By entering a try block. If an error gets detected within the try block then the exception handler manages the error.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you catch an exception?

A

Throw an exception within a try block and catch the exception with the matching type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you “install” a terminate function?

A

Create a function that has no arguments and no return type with whatever you want the termination to do (i.e., output a message). The function must have an “exit(1)” at the end. Then in the scope (such as main) you would like to have the program terminate upon an error, call the “set_terminate” function with your made function as an argument, thus, “installing the terminate function. Upon an error being thrown that would cause the program to abort, your function will activate and exit the program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Whaat happens if you ONLY throw an exception and an error is detected (no try/catch blocks, no set_terminate)?

A

The program aborts.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Why is exception handling useful?

A

It improves the structure, organization, and reusability of our software.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is handling and exception?

A

When the exception is transferred to another point in the program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the throw point?

A

The point an error is detected.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly