4-Exceptions & Memory Flashcards

1
Q

Exception handling is used when

A

the system can recover from the error causing the exception

so permits to catch and handle errors

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

What happens if there’s no catch block for the exception

A
  • Unwinding the stack, terminate() -> abord(): quits the program
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

If a function throws a const object, what should the catch handler argument type be?

A

It must be declared as a const

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

If a function is provided a list of exceptions to throw, what could possibly go wrong?

A

If an exception not in the list is thrown, the unexpected exception handler is invoked, which terminates the program (Not recommended).

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

Exceptions thrown in constructors (copy cons and destructor)

A
  • In order to catch the exception, the exception handler must have access to a copy cons for the thrown object
  • Exceptions thrown in cons cause destructors to be called for any objects built as part of the object.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

The new operator behavior (single and array objects)

A

singles
- allocates enough memory to hold an object: if not, throws an exception
- then, it calls the cons to initialize the object in memory that was allocated
arrays
- new[] is called and allocates memory for all elements of the array
string *ps = new string[10]
- delete[]: if only delete is callled, it will only deconstruct the first object

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

What happends if String *title = new String(“kicks”) throws an exception

A

String *title = new String(“kicks”)

if string cons threw exception: if new succeded, then delete needs to be called on the allocated, but uninitialized storage.
if new threw exception: then no memory was allocated and operator delete should not be thrown

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