Managing CTRL+C in console application

Normally, if the user uses the CTRL+C keys, the Eiffel application detect it as an error and throw an exception of type OPERATING_SYSTEM_SIGNAL_FAILURE.

To manage the CTRL+C keys, you can use a rescue clause to detect the exception and a retry mechanism to cancel the exception handling done by the Eiffel runtime.

To detect the exception, you can inherit from the EXCEPTIONS class and use an attachment test on Exception_manager.last_exception.

Note that this code does not work on Windows. If used on Windows, the application will quit, but the rescue call is not launched.

note description: "Show how to quit an application using CTRL+C (without trace)." author: "Louis Marchand" date: "Wed, 25 Apr 2018 23:12:33 +0000" revision: "0.1" class APPLICATION inherit EXCEPTIONS create make feature {NONE} -- Initialization make -- Launch `Current'. local l_ctrl_c:BOOLEAN do if not l_ctrl_c then from until False loop io.standard_default.put_string ("Press CTRL+C%N") io.input.read_line end else io.standard_default.put_string ("%NClosing...%N") end rescue if attached {OPERATING_SYSTEM_SIGNAL_FAILURE} Exception_manager.last_exception then l_ctrl_c := True retry end end end

cached: 03/29/2024 2:03:01.000 AM