# HG changeset patch # User John W. Eaton # Date 1248373485 14400 # Node ID 232a0ddce7cd15edd6442bf5e3323f629899dde9 # Parent 3ab05b7b61ee8580cd5506ffe9d0d538f1aacedf toplev.cc: handle exceptions while preparing to exit diff -r 3ab05b7b61ee -r 232a0ddce7cd src/ChangeLog --- a/src/ChangeLog Wed Nov 18 20:18:47 2009 -0500 +++ b/src/ChangeLog Thu Jul 23 14:24:45 2009 -0400 @@ -1,3 +1,9 @@ +2009-07-23 John W. Eaton + + * toplev.cc (IGNORE_EXCEPTION, SAFE_CALL): New macros. + (clean_up_and_exit, do_octave_atexit): Use SAFE_CALL to handle + exceptions while preparing to exit. + 2009-08-04 John W. Eaton * toplev.cc (octave_call_stack::do_goto_frame_relative): Allow diff -r 3ab05b7b61ee -r 232a0ddce7cd src/toplev.cc --- a/src/toplev.cc Wed Nov 18 20:18:47 2009 -0500 +++ b/src/toplev.cc Thu Jul 23 14:24:45 2009 -0400 @@ -647,6 +647,25 @@ return retval; } +// Call a function with exceptions handled to avoid problems with +// errors while shutting down. + +#define IGNORE_EXCEPTION(E) \ + catch (E) \ + { \ + std::cerr << "error: ignoring " #E " while preparing to exit" << std::endl; \ + recover_from_exception (); \ + } + +#define SAFE_CALL(F, ARGS) \ + try \ + { \ + F ARGS; \ + } \ + IGNORE_EXCEPTION (octave_interrupt_exception) \ + IGNORE_EXCEPTION (octave_execution_exception) \ + IGNORE_EXCEPTION (std::bad_alloc) + // Fix up things before exiting. void @@ -654,7 +673,7 @@ { do_octave_atexit (); - sysdep_cleanup (); + SAFE_CALL (sysdep_cleanup, ()) if (octave_exit) (*octave_exit) (retval == EOF ? 0 : retval); @@ -991,26 +1010,11 @@ octave_atexit_functions.pop_front (); - reset_error_handler (); + SAFE_CALL (reset_error_handler, ()) - try - { - feval (fcn, octave_value_list (), 0); - } - catch (octave_interrupt_exception) - { - recover_from_exception (); - } - catch (octave_execution_exception) - { - recover_from_exception (); - } - catch (std::bad_alloc) - { - recover_from_exception (); - } + SAFE_CALL (feval, (fcn, octave_value_list (), 0)) - flush_octave_stdout (); + SAFE_CALL (flush_octave_stdout, ()) } if (! deja_vu) @@ -1020,23 +1024,23 @@ // Do this explicitly so that destructors for mex file objects // are called, so that functions registered with mexAtExit are // called. - clear_mex_functions (); + SAFE_CALL (clear_mex_functions, ()) - command_editor::restore_terminal_state (); + SAFE_CALL (command_editor::restore_terminal_state, ()) // FIXME -- is this needed? Can it cause any trouble? - raw_mode (0); + SAFE_CALL (raw_mode, (0)) - octave_history_write_timestamp (); + SAFE_CALL (octave_history_write_timestamp, ()) if (Vsaving_history) - command_history::clean_up_and_save (); + SAFE_CALL (command_history::clean_up_and_save, ()) - close_files (); + SAFE_CALL (close_files, ()) - cleanup_tmp_files (); + SAFE_CALL (cleanup_tmp_files, ()) - flush_octave_stdout (); + SAFE_CALL (flush_octave_stdout, ()) if (! quitting_gracefully && (interactive || forced_interactive)) { @@ -1045,7 +1049,7 @@ // Yes, we want this to be separate from the call to // flush_octave_stdout above. - flush_octave_stdout (); + SAFE_CALL (flush_octave_stdout, ()) } } }