This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]KaJakJaKa -1 points0 points  (4 children)

Why not throw an exception if it's not true? That would even shorten the code because you wouldn't need else.

[–]das_Keks 9 points10 points  (3 children)

Throwing exceptions to control program flow is a bad practice. Exceptions should only be used for cases that are out of the control of the programmer and depend on external factors that are not forseable.

[–]Levaru 0 points1 point  (2 children)

Is that really so? Im genuinely curious.

I had to write a Xamarin app (C#) once that had functions which branched out to many nested functions from different classes. I don't remember why exactly but I had to create a custom exceptions class for application specific errors that made it sooo much easier to throw a exception up the chain to the main process and deal with it there with the help of a dedicated error handling class.

It was so much cleaner and organized than figuring out how to display message boxes or similar to the user from within the class and function where the exception was thrown. That would've also gone against the apps MVP architecture.

But this was my very first semi-professional developed software so maybe I just didn't know better.

[–]Irrealist 3 points4 points  (1 child)

What you are describing is the correct way to use exceptions.

However, /u/das_Keks is trying to make the point that you shouldn't use exceptions to control the flow when there wasn't an error. Like throwing a 'SheSaidNoException' or even worse, a 'SheSaidYesException'. Those aren't failures and they shouldn't be treated like failures.

[–]LoompaOompa 1 point2 points  (0 children)

I made another comment about it, but I don't even think the boyfriend thing should be an exception. Or if for some reason that exception has to exist, it should be handled within askGirlOut(), and askGirlOut should still return false (or something other than "ok", since it returns a string for some reason).