Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using exceptions for non-error purposes

Is it a good practice to use exception for managing cases that are not errors ?

Like in JavaScript and Python that manage the StopIteration case in generators (yield keyword).

like image 475
Franck Freiburger Avatar asked Jul 21 '09 14:07

Franck Freiburger


2 Answers

I would say No, spontaneously. Exceptions should be used for exceptional states, not for regular program flow.

like image 65
Fredrik Mörk Avatar answered Nov 15 '22 06:11

Fredrik Mörk


Not usually, no. ASP.NET uses this for Response.Redirect - actually aborting the thread, then catching the exception and resetting it. It's very nasty, but it does let you basically "quit" the request without every level of the stack being aware that it needs to return immediately.

Try to avoid wherever possible. If you think you absolutely have to do it, consult two colleagues, present the design and ask if they can do it more cleanly somehow. If none of you can think of a better way round it, do it with extensive documentation.

like image 24
Jon Skeet Avatar answered Nov 15 '22 05:11

Jon Skeet