How to execute cleanup code on Ctrl-C (Break)?

Jonathan M Davis jmdavisProg at gmx.com
Sun Jul 8 13:10:47 PDT 2012


On Sunday, July 08, 2012 18:57:17 Paul Dufresne wrote:
> I am trying to translate some C++ code to D to learn D.
> 
> And the first lines to translate are:
> signal(SIGINT, (sighandler_t) shutdownClient);
> signal(SIGTERM, (sighandler_t) shutdownClient);
> 
> So I come to try:
> import std.stdio;
> void main(){
>      try {
> 	  while (true) writeln("H1!!");
> 	} finally writeln("oh... Bye then!");
> }
> 
> Using 2.059 dmd on Windows.
> I expected the "ob... Bye then!" to show up on Ctrl-C, but it did
> not:
> H1!!
> H1!!
> H1!!
> H1!!
> ^C
> C:\Users\Paul\myd>
> 
> I have seens that there is std.signals but that does not really
> seems related.
> 
> Help!

I wouldn't expect an exception from ctrl-c. That sends a signal to kill the 
program, which normally means that you're program is killed without any 
exceptions being thrown or anything of the sort. You just get to do something 
in the signal handler if you catch the kill signal and _then_ the program 
dies. But maybe Windows does something differently. I don't know. I use Linux 
almost exclusively, and you definitely _don't_ get an exception in Linux.


More information about the Digitalmars-d-learn mailing list