/tmp>cat error.c #include #include #include #include int main(int argc, char ** argv) { int fd; char buf[100]; ssize_t n; fd = open("foo", O_RDONLY); if (fd == -1) { perror("failed to open foo"); /* evaluates errno */ return 1; } n = read(fd, buf, sizeof(buf)); if (n == -1) { perror("failed to read from foo"); return 2; } if (n != sizeof(buf)) { fprintf(stderr, "Short read from foo (got %z, expected %z)\n", n, sizeof(buf)); return 3; } if (close(fd) == -1) { perror("failed to close foo"); return 4; } // and so on ... } /tmp>make error cc error.c -o error /tmp>./error failed to open foo: No such file or directory /tmp> #### try { line = console.readLine(); if (line.length() == 0) { throw new EmptyLineException("The line read from console was empty!"); } console.printLine("Hello %s!" % line); } catch (EmptyLineException e) { console.printLine("Hello!"); } catch (Exception e) { console.printLine("Error: " + e.message()); } else { console.printLine("The program ran successfully."); } finally { console.printLine("The program is now terminating."); } #### use Try::Tiny; $dbh->{AutoCommit} = 0; # enable transactions, if possible $dbh->{RaiseError} = 1; try { foo(...) # do lots of work here bar(...) # including inserts baz(...) # and updates $dbh->commit; # commit the changes if we get this far } catch { warn "Transaction aborted because $_"; # Try::Tiny copies $@ into $_ # now rollback to undo the incomplete changes # but do it in an eval{} as it may also fail eval { $dbh->rollback }; # add other application on-error-clean-up code here }; #### $h->method(@args) or die $h->errstr;