#How to handle errors
26 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
In practice I have hardly ever seen the explicit use of exit()
IIRC exit was not for failures, thats std::terminate
And honestly apart from the standard I have seen mixed feelings about exceptions in C++
Usually you’d use them for non logic errors, those that you can’t handle
what do you do when you have an error and you dont use exceptions ?
Let's say you only want positive integers as input and the user gives you negative values
I guess you can repeatedly ask the user for new input until they input something correct
I personally wouldn't use an exception for such a case
return std::variant<ExpectedResultType, ErrorType>
Std expected id you're using cpp23
std::variant since C++17
ez
use exceptions when u want the program to entirely crash
otherwise use std::expected or u can build one urself it's ez and have error handling with less overhead
std::pair or just write your own ™️
std::pair takes space for both of these return values, you can't decide which one is actually used, but with std::variant there is only one type that is saved in the instance
because it is a type safe union
oh cool
Write a wrapper arohnd a union instrwd of a std pair
wat? crashing the program is the exact opposite of what exceptions are for. I guess you're confusing exceptions with assertions or smth.
as a general rule of thumb, you never use exit() in C++.
exit() doesn't unwind the stack, destructors won't be called, which is why exit() is basically malpractice in C++
use exceptions for dealing with errors that are not expected to happen during normal program execution or can't be handled locally
This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.