#How to handle errors

26 messages · Page 1 of 1 (latest)

small plover
#

Hey I have a few questions about handling errors in c++
When do you use exit() or c++ exceptions ?
Are there any scenarios when you want to throw an exception and not catch the exception then exit safely ?
If yes, when do you use exit() over throw ?
Are there other alternatives than the ones I just listed to handle errors ?

bronze stoneBOT
#

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.

velvet hamlet
#

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

small plover
#

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

velvet hamlet
#

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

coarse vapor
cold zinc
coarse vapor
#

std::variant since C++17

cold zinc
cloud ravine
#

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

cloud ravine
coarse vapor
#

because it is a type safe union

cloud ravine
#

oh cool

cold zinc
#

Write a wrapper arohnd a union instrwd of a std pair

tender arrow
tender arrow
#

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

bronze stoneBOT
#

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.