Is my method of void replacement bad idea?

Since I really don't like exception throwing, I came up with the idea where instead of a void I return class PorcessInfo. ProcessInfo is a class which contains variable of process status (success/failure) and message of the process like "Process was successful" or "Can't load lib". In my program, every void returns it. Is this good idea? Maybe I don't need to use it every time, but in some cases, it's very useful.

Here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
enum ProcessStatuses {

	PROCESS_SUCCESS, PROCESS_FAILURE, PROCESS_NULL

};

class ProcessInfo {

	ProcessStatuses process_status_ = PROCESS_NULL;

	std::string message_;

	void setProcessStatus(ProcessStatuses process_status) {

		process_status_ = process_status;

	}

	void setMessage(const char *message) {

		message_ = message;

	}

public:

	ProcessInfo() = default;

	ProcessInfo(ProcessStatuses process_status, const char *message) {

		setProcessStatus(process_status);
		setMessage(message);

	}

	bool operator==(const ProcessStatuses &parameter) const {

		return process_status_ == parameter;
	}

	bool operator!=(const ProcessStatuses &parameter) const {

		return process_status_ != parameter;
	}

	ProcessStatuses getProcessStatus() {

		return process_status_;
	}

	std::string getMessage() {

		return message_;
	}

};
Last edited on
One advantage of an exception is that an error can't be silently ignored altogether.

In C++17, a function returning a value which should be checked by the caller
can be adorned with the attribute [[nodiscard]]

1
2
// caller should be warned if the result of calling this function is ignored
[[nodiscard]] ProcessStatus do_something() ;

http://coliru.stacked-crooked.com/a/7e5af0691c2eb35c
Just be sure you won't go further developing your ideas until after an year you finally reimplement exceptions and find out that you wasted one year of time ;)

Jokes aside, i can't say that avoiding using a language feature which exists to do a specific thing just because you don't like it, while being in the exact context that feature is built to handle, is a good habit.

That said, as long as you're 100% consistent with your self defined structure, it doesn't seem to be much of a problem. Just don't overcomplicate things when you already have a tool at your disposal which does exactly what you need just because you don't want to see the word "exception". And that's a thing i say for everything, same for gotos.

And yeah if your compiler allows use the [[nodiscard]] attribute on all the functions where you apply that solution.

Moreover, if you really want it to be totally consistent, you should rather have all your methods return a ProcessStatus, and put the Return value on an argument passed by reference.
Last edited on
Topic archived. No new replies allowed.