On arg null, is it better to compromise or throw an exception?

Possible duplicate:
contract test design with approval or exclusion?

What is the preferred way to handle the null pointer passed as the output argument to the function? I could RESPECT, but I feel that it is not good to let the library crash the program. Instead, I thought about using exceptions.

+3
source share
8 answers

Throw an exception! What are they needed for. Then your library user can decide whether they want to handle it gracefully or crash and burn it.

- , , , .

+11

, . , , . , , . , , "" .

, ( ), assert , , ( undefined) , . , strlen, , vector<>::at. , undefined .

, "" ?

try {
  process(data);
} catch(NullPointerException &e) {
  process(getNonNullData());
}

, -, . , ,

if(!data) {
  process(getNonNullData());
} else {
  process(data);
}

, , ( , NULL, ). , process, , ( ).

assert. , -NULL.

+3

.

, , , , .

- , , .

( , , , - @RogerPate ):

#define require_not_null(ptr) \
    do { assert(ptr); if (!(ptr)) throw std::logic_error("null ptr"); } while (0)
+2

, . , , , , , . .

. , , , ? , , , - . , , - .

, , , , "out".

+1

, .

, Eiffel " " ( ), . , .

+1

, exit ...

, .

, , .

+1

assert, ++ Standard. strlen(). , ? . - , NULL , undefined.

+1

, , . , , , . , , NULL , . , , , NULL , , , . - .

0
source

Source: https://habr.com/ru/post/1747641/


All Articles