Another alternative, closer to the switch syntax David proposed earlier:
try { Stdio.format_filesystem("/dev/hda2", "ext5"); } catch (Exception e) { case e->invalid_file_system_type: // invalid file system type break; case e->no_permission || e->not_mounted: // no permission or filesystem mounted break; }
The main feature with this is compared to "try (Exception e) {...} catch ..." is that it avoids the awkward question whether the variable e is visible inside the try block or not. The problem is that it also allows fall-through, which can be a source of mistakes.
/ Martin Stjernholm, Roxen IS
Previous text:
2003-10-01 18:09: Subject: Re: throw or return
A better exception system has been on the wish list for a long time. I agree that changing the errno stuff to exceptions before we have that would be a mistake. Any takers to do something about it?
I think a more "pike:ish" way to implement it is to use recognition constants instead of type inference. That makes it simpler to fix correct exception objects in dynamic environments, and it follows the "looks-like" rather than "is-a" philosophy of type comparisons in Pike.
Anyway, the difference from your proposal would be mostly syntactic in practice:
try (Exception e) { Stdio.format_filesystem("/dev/hda2", "ext5"); } catch (e->invalid_file_system_type) { // invalid file system type } catch (e->no_permission || e->not_mounted) { // no permission or filesystem mounted }
Note that the exception variable is specified in the try clause, much like how foreach variables are specified or declared in foreach clauses. The catch clauses take normal expressions, which means full freedom in writing complex catch conditions.
One could perhaps support a shorthand syntax to avoid handling the exception object explicitly:
try { Stdio.format_filesystem("/dev/hda2", "ext5"); } catch invalid_file_system_type { // invalid file system type } catch no_permission, not_mounted { // no permission or filesystem mounted }
/ Martin Stjernholm, Roxen IS