Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Can ctrl-c throw an exception?

912 views
Skip to first unread message

klydefrog

unread,
Mar 16, 2009, 11:34:58 AM3/16/09
to
Can I set things up so that if I hit ctrl-c while a program is running, then a catch block will be run before of a hard termination of the script?

Thanks.

Matt Fig

unread,
Mar 16, 2009, 12:37:11 PM3/16/09
to
Why not give it a try?

function [] = ctrlcthrow
% Call the function, then hit ctrl+c.

cnt = 1;
try
while true
cnt = cnt +1;
end
catch
disp('Error caught')
end

Steven Lord

unread,
Mar 16, 2009, 12:50:30 PM3/16/09
to

"klydefrog" <aj00m...@gmail.com> wrote in message
news:18909815.1237217764...@nitrogen.mathforum.org...

> Can I set things up so that if I hit ctrl-c while a program is running,
> then a catch block will be run before of a hard termination of the script?
>
> Thanks.

No. In older releases, Ctrl-C could be caught by TRY/CATCH; that changed a
while ago. [When Ctrl-C was caught, you could get into a situation where
there was no way, short of terminating the MATLAB process, to end your
code.] Ctrl-C is one of the bigger hammers -- it will terminate the
currently running program and return to the command prompt at the next
opportunity MATLAB can do so.

However, if you want to do some sort of cleanup after Ctrl-C but before
MATLAB returns to the prompt (say to close a file or a figure window) then
you can use an onCleanup object to do so.

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/oncleanup.html

--
Steve Lord
sl...@mathworks.com


klydefrog

unread,
Mar 16, 2009, 1:43:52 PM3/16/09
to
That oncleanup does look like I want. However, I'm using 2006b, and it seems that it does not exist. :(

Steven Lord

unread,
Mar 17, 2009, 1:34:29 PM3/17/09
to

"klydefrog" <aj00m...@gmail.com> wrote in message
news:8894173.12372254636...@nitrogen.mathforum.org...

> That oncleanup does look like I want. However, I'm using 2006b, and it
> seems that it does not exist. :(

That's correct, onCleanup was introduced in MATLAB 7.6 (R2008a).

http://www.mathworks.com/access/helpdesk/help/techdoc/rn/brjk1q5-1.html#brjk1q5-9

I can't really think of a good way for you to perform some of your own
cleanup after a Ctrl-C in release R2006b. MATLAB will clean up the
variables that are in the function workspace, but doesn't perform cleanup
like closing file handles or the like.

--
Steve Lord
sl...@mathworks.com


Christian

unread,
Jan 15, 2013, 4:05:08 AM1/15/13
to
> No. In older releases, Ctrl-C could be caught by TRY/CATCH; that changed a
> while ago. [When Ctrl-C was caught, you could get into a situation where
> there was no way, short of terminating the MATLAB process, to end your
> code.] Ctrl-C is one of the bigger hammers -- it will terminate the
> currently running program and return to the command prompt at the next
> opportunity MATLAB can do so.

That sound perfectly reasonable. Is there any "smaller hammer" to make a running function throw a catchable error?

What I want to do is allowing to abort a subroutine and continue with the next when the user sees (for example at the display of an external device) that it's not needed to continue with the current subroutine.

Thanks

Steven_Lord

unread,
Jan 15, 2013, 1:39:02 PM1/15/13
to


"Christian " <christian...@sma.de> wrote in message
news:kd3644$r2o$1...@newscl01ah.mathworks.com...
>> No. In older releases, Ctrl-C could be caught by TRY/CATCH; that changed
>> a while ago. [When Ctrl-C was caught, you could get into a situation
>> where there was no way, short of terminating the MATLAB process, to end
>> your code.] Ctrl-C is one of the bigger hammers -- it will terminate the
>> currently running program and return to the command prompt at the next
>> opportunity MATLAB can do so.
>
> That sound perfectly reasonable. Is there any "smaller hammer" to make a
> running function throw a catchable error?

Clarification: do you mean you have a function that's been running for a
while and you want to force it to throw an error that its caller's CATCH
block can catch without modifying the running function? Or do you want to
modify the function so that it throws an error when certain conditions are
satisfied?

For the former meaning, I don't believe so unless you wrote your code
originally with that goal in mind (checking the status of a button in a GUI
or the existing of a "stop!" file, etc.) or if your code "looks for
something" that you can affect without interacting with the main MATLAB
process (closing a figure, deleting a file on disk, etc.)

For the latter, just call ERROR.

> What I want to do is allowing to abort a subroutine and continue with the
> next when the user sees (for example at the display of an external device)
> that it's not needed to continue with the current subroutine.

For that, I would have a dialog with a "Stop" button and have the code
periodically test if the button has been pressed. Have the code RETURN or
ERROR if the button has been pressed.

--
Steve Lord
sl...@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Christian

unread,
Jan 16, 2013, 2:07:14 AM1/16/13
to
> For that, I would have a dialog with a "Stop" button and have the code
> periodically test if the button has been pressed. Have the code RETURN or
> ERROR if the button has been pressed.

That's exactly what I'm doing now. Since there is no single line of code executing periodically it's a bit of a mess. Just pressing CTRL-C was somewhat more comfortable but changing to a newer version sometimes comes at a price...

Thanks anyway

Bruno Luong

unread,
Jan 16, 2013, 2:27:09 AM1/16/13
to
"Christian" wrote in message <kd5jj2$87p$1...@newscl01ah.mathworks.com>...
> > For that, I would have a dialog with a "Stop" button and have the code
> > periodically test if the button has been pressed. Have the code RETURN or
> > ERROR if the button has been pressed.
>
> That's exactly what I'm doing now. Since there is no single line of code executing periodically it's a bit of a mess.

Not justified as argument. Why not check at the same time where the graph/displayed is updated? That's the place where the user can see something new happens and decides to stop or pursue the process.

Just centralize the plotting in one customized function then check the button state inside.

Bruno

Christian

unread,
Jan 16, 2013, 7:50:08 AM1/16/13
to
> Not justified as argument. Why not check at the same time where the graph/displayed is updated? That's the place where the user can see something new happens and decides to stop or pursue the process.

No. In our use case the user can look on the displays of external instruments with displays of their own which are not updated by matlab but make their own measurements (like voltage and temperature). So the user can sometimes determine that a certain pass is useless while matlab is doing something completely different.

Bruno Luong

unread,
Jan 16, 2013, 8:07:08 AM1/16/13
to
You might be interested in onCleanup. Put the below in a single file foo.m, run it then hit Ctrl-C key.

Bruno

%%
function foo
onCleanup(@bar);
while true
end % infinity loop
end % foo

%%
function bar(varargin);
disp('Go here before exit');
end % bar
0 new messages