scriptlike icon indicating copy to clipboard operation
scriptlike copied to clipboard

The Fail Exception should take a return code

Open JesseKPhillips opened this issue 11 years ago • 2 comments

It can be very important to specify the error of a program run, so fail should easily specify what that code should be. Maybe even type check that it is an enum to force users to name their errors :)

JesseKPhillips avatar Feb 12 '14 01:02 JesseKPhillips

I like this, but I'm not sure if there's a way to do it. The way fail() works is by throwing an exception (Fail) which is designed to print a custom string instead of a stack trace after it's left uncaught. This provides three benefits:

  • Functions other than main() can decide to trigger "exit the program".
  • Any finally/scope(...) cleanup code on the stack still gets run.
  • None of this requires the user to add any boilerplate/scaffolding to main().

I'm not aware of a way to support custom exit codes without losing at least one of those benefits.

Unless it turns out there really is some way for an exception to say "If I'm not caught, then exit with THIS custom status code instead of 1" (anyone know?), then I think the best I could do is ask users who want the custom exit code feature to actually work to add a mixin(setupCustomExitCode); at the top of their main(). Would that be worth it though?

Abscissa avatar Jan 31 '17 03:01 Abscissa

I see, probably could make some sense if Exceptions could specify their return code in D itself. While I like to try and specify an exit code on failure, I haven't really ever made use of it beyond was this execution successful. Robocopy being really annoying as several exit codes are still considered a successful run.

JesseKPhillips avatar Feb 08 '17 22:02 JesseKPhillips