"Try? There is no try." -- Yoda's Exception handling syntax
[email protected] ("David L. Nicol") Tue, 15 Aug 2000 20:11:55 +0000
| Newsgroups | perl.perl6.language.flow |
|---|---|
| Organization | University of Missouri - Kansas City supercomputing infrastructure |
| Message-ID | <[email protected]> |
> Maintainer: Peter Scott <[email protected]> > Date: 8 Aug 2000 > Last-Modified: 14 Aug 2000 > Version: 3 > Mailing List: [email protected] > Number: 63 > An exception handling mechanism is proposed with the following syntax: > > exception EXCEPTION > > try BLOCK catch [EXCEPTION [, ...] ] BLOCK ... > try BLOCK catch [EXCEPTION [, ...] ] BLOCK continue BLOCK > try BLOCK continue BLOCK > > throw EXCEPTION [ (attributes) ] > What if "try" were implied by the appearance of "catch" keywords, which expire at the close of their block? catch [EXCEPTION [, ...] ] BLOCK ,... BLOCK > exception Exception::MyDB; > sub foo { > open_database(); > # database-munging code > throw Exception::MyDB(message => 'permission denied'); > close_database(); > open_network_connection(); > # network munging code > throw Exception::IO(message => 'timeout'); # In Socket.pm, perhaps > close_network_connection(); > } > > try { > foo(); > } catch Exception::MyDB, Exception::DBI { > close_database(); > # other stuff > } catch Exception::IO { > close_network_connection(); > # other stuff > } catch { > my $err = shift; > warn "Some other kind of error in foo: $err\n"; > throw $err; # Re-throw exception (just 'cuz we wanted to) > } the block would become: { # prepare to call foo() catch Exception::MyDB, Exception::DBI { close_database(); # other stuff } catch Exception::IO { close_network_connection(); # other stuff } catch { my $err = shift; warn "Some other kind of error in foo: $err\n"; throw $err; # Re-throw exception (just 'cuz we wanted to) } foo(); }; The reason is, this way, we don't have to keep track of, are we in a try or not, while running. We always are. An error happens, we back out of blocks until we find an appropriate catch if any. Retrying can be done with (failure coutners and) recursion or GOTOs. I believe that "try" occurs in C++ to mark the enclosed block as requiring special handling regarding stack unwinding. Since the perl stack is different than a C++ stack, we may not need to specify "try." Would someone please enlighten me as to the purpose of an explicit "try."