comparison src/octave.cc @ 11368:944b7e20fc5a

improve handling of exceptions for startup files
author John W. Eaton <jwe@octave.org>
date Wed, 15 Dec 2010 02:05:27 -0500
parents 2718e1fdf82f
children 141b3fb5cef7
comparison
equal deleted inserted replaced
11367:b2191ebea12f 11368:944b7e20fc5a
273 args(0) = "GNU Octave"; 273 args(0) = "GNU Octave";
274 274
275 F__version_info__ (args, 0); 275 F__version_info__ (args, 0);
276 } 276 }
277 277
278 static void
279 gripe_safe_source_exception (const std::string& file, const std::string& msg)
280 {
281 std::cerr << "error: " << msg << "\n"
282 << "error: execution of " << file << " failed\n"
283 << "error: trying to make my way to a command prompt"
284 << std::endl;
285 }
286
287 // Execute commands from a file and catch potential exceptions in a
288 // consistent way. This function should be called anywhere we might
289 // parse and execute commands from a file before before we have entered
290 // the main loop in toplev.cc.
291
292 static void
293 safe_source_file (const std::string& file_name,
294 const std::string& context = std::string (),
295 bool verbose = false, bool require_file = true,
296 const std::string& warn_for = std::string ())
297 {
298 try
299 {
300 source_file (file_name, context, verbose, require_file, warn_for);
301 }
302 catch (octave_interrupt_exception)
303 {
304 recover_from_exception ();
305 octave_stdout << "\n";
306 if (quitting_gracefully)
307 clean_up_and_exit (exit_status);
308 }
309 catch (octave_execution_exception)
310 {
311 recover_from_exception ();
312 gripe_safe_source_exception (file_name, "unhandled execution exception");
313 }
314 catch (std::bad_alloc)
315 {
316 recover_from_exception ();
317 gripe_safe_source_exception
318 (file_name,
319 "memory exhausted or requested size too large for range of Octave's index type");
320 }
321 }
322
278 // Initialize by reading startup files. 323 // Initialize by reading startup files.
279 324
280 static void 325 static void
281 execute_startup_files (void) 326 execute_startup_files (void)
282 { 327 {
297 // Execute commands from the site-wide configuration file. 342 // Execute commands from the site-wide configuration file.
298 // First from the file $(prefix)/lib/octave/site/m/octaverc 343 // First from the file $(prefix)/lib/octave/site/m/octaverc
299 // (if it exists), then from the file 344 // (if it exists), then from the file
300 // $(prefix)/share/octave/$(version)/m/octaverc (if it exists). 345 // $(prefix)/share/octave/$(version)/m/octaverc (if it exists).
301 346
302 source_file (Vlocal_site_defaults_file, context, verbose, require_file); 347 safe_source_file (Vlocal_site_defaults_file, context, verbose,
303 348 require_file);
304 source_file (Vsite_defaults_file, context, verbose, require_file); 349
350 safe_source_file (Vsite_defaults_file, context, verbose, require_file);
305 } 351 }
306 352
307 if (read_init_files) 353 if (read_init_files)
308 { 354 {
309 // Try to execute commands from $HOME/$OCTAVE_INITFILE and 355 // Try to execute commands from $HOME/$OCTAVE_INITFILE and
323 369
324 std::string local_rc; 370 std::string local_rc;
325 371
326 if (! home_rc.empty ()) 372 if (! home_rc.empty ())
327 { 373 {
328 source_file (home_rc, context, verbose, require_file); 374 safe_source_file (home_rc, context, verbose, require_file);
329 375
330 // Names alone are not enough. 376 // Names alone are not enough.
331 377
332 file_stat fs_home_rc (home_rc); 378 file_stat fs_home_rc (home_rc);
333 379
345 if (! home_rc_already_executed) 391 if (! home_rc_already_executed)
346 { 392 {
347 if (local_rc.empty ()) 393 if (local_rc.empty ())
348 local_rc = octave_env::make_absolute (initfile); 394 local_rc = octave_env::make_absolute (initfile);
349 395
350 source_file (local_rc, context, verbose, require_file); 396 safe_source_file (local_rc, context, verbose, require_file);
351 } 397 }
352 } 398 }
353 } 399 }
354 400
355 static int 401 static int
384 recover_from_exception (); 430 recover_from_exception ();
385 octave_stdout << "\n"; 431 octave_stdout << "\n";
386 if (quitting_gracefully) 432 if (quitting_gracefully)
387 clean_up_and_exit (exit_status); 433 clean_up_and_exit (exit_status);
388 } 434 }
435 catch (octave_execution_exception)
436 {
437 recover_from_exception ();
438 std::cerr << "error: unhandled execution exception -- eval failed"
439 << std::endl;
440 }
389 catch (std::bad_alloc) 441 catch (std::bad_alloc)
390 { 442 {
391 std::cerr << "error: memory exhausted or requested size too large for range of Octave's index type -- eval failed" 443 std::cerr << "error: memory exhausted or requested size too large for range of Octave's index type -- eval failed"
392 << std::endl; 444 << std::endl;
393 } 445 }
436 std::string tmp = (pos != std::string::npos) 488 std::string tmp = (pos != std::string::npos)
437 ? curr_fcn_file_name.substr (pos+1) : curr_fcn_file_name; 489 ? curr_fcn_file_name.substr (pos+1) : curr_fcn_file_name;
438 490
439 octave_program_name = tmp; 491 octave_program_name = tmp;
440 492
441 try 493 std::string context;
442 { 494 bool verbose = false;
443 std::string context; 495 bool require_file = true;
444 bool verbose = false; 496
445 bool require_file = true; 497 safe_source_file (fname, context, verbose, require_file, "octave");
446
447 source_file (fname, context, verbose, require_file, "octave");
448 }
449 catch (octave_interrupt_exception)
450 {
451 recover_from_exception ();
452 octave_stdout << "\n";
453 if (quitting_gracefully)
454 clean_up_and_exit (exit_status);
455 }
456 catch (std::bad_alloc)
457 {
458 std::cerr << "error: memory exhausted or requested size too large for range of Octave's index type -- execution of "
459 << fname << " failed" << std::endl;
460 }
461 } 498 }
462 499
463 // Usage message with extra help. 500 // Usage message with extra help.
464 501
465 static void 502 static void