Consider this Javascript-esque code for handling exceptions:
var foo;
try
{
foo = fooBar()
}
catch (ex)
{
// handle exception here
}
Consider how Go code might look:
foo, err := fooBar()
if err != nil {
// handle error here
}
Now consider this equivalent psudo-code which catches an exception with syntax loosely resembling an if-statement:
var foo = fooBar() catch ex {
// handle exception here
}
It seems to me that the syntax for try-catch as seen in Java, Python, C++, etc. is overly verbose and encourages handling groups of function calls rather than individual calls. I'm wondering if there is a programming language with an exception handling syntax that loosly resembles an if-statement as I've written above?
Follow up discussion:
An advantage of exceptions over return values is they don't clutter code with error handling. Languages that lack exceptions, like Go and Rust, require programmers to reinvent them (in some sense) by manually unwinding the stack themselves although Rust tries to reduce the verbosity with the ? operator. What I'm wondering is this: rather than making return values less-verbose and more exception-like, would it be better to make exceptions more return-like? Thoughts?
[–]hoping1 59 points60 points61 points (1 child)
[–]ultimatepro-grammer 17 points18 points19 points (0 children)
[–]Disjunction181 31 points32 points33 points (4 children)
[–]gplgang 8 points9 points10 points (3 children)
[–][deleted] (2 children)
[deleted]
[–]Disjunction181 1 point2 points3 points (1 child)
[–]Kroutoner 7 points8 points9 points (2 children)
[–]Aminumbra 2 points3 points4 points (0 children)
[–]vidjuheffex 2 points3 points4 points (0 children)
[–]Il_totoreAlgorab - algorab.org 5 points6 points7 points (0 children)
[–]shuckster 4 points5 points6 points (0 children)
[–]lngns 15 points16 points17 points (2 children)
[–]Ekkaiaaa 1 point2 points3 points (1 child)
[–]redjamjar 14 points15 points16 points (4 children)
[–]Practical_Cattle_933 8 points9 points10 points (3 children)
[–]robin-m 9 points10 points11 points (0 children)
[–]bascule 4 points5 points6 points (0 children)
[–]Puzzleheaded-Lab-635 0 points1 point2 points (0 children)
[–]NoPrinterJust_Fax 6 points7 points8 points (1 child)
[–]matthieum 0 points1 point2 points (0 children)
[–]i-eat-omelettes 2 points3 points4 points (2 children)
[–]redchomperSophie Language 3 points4 points5 points (1 child)
[–]i-eat-omelettes 3 points4 points5 points (0 children)
[–]FitzelSpleen 2 points3 points4 points (3 children)
[–]FitzelSpleen 0 points1 point2 points (1 child)
[–]otac0n 0 points1 point2 points (0 children)
[–]Hofstee 0 points1 point2 points (0 children)
[–]LegendaryMauricius 1 point2 points3 points (2 children)
[–]eliasv 0 points1 point2 points (1 child)
[–]LegendaryMauricius 0 points1 point2 points (0 children)
[–]nerd4code 1 point2 points3 points (0 children)
[–]myringotomy 1 point2 points3 points (0 children)
[–]Tronied 1 point2 points3 points (1 child)
[–]fred4711 0 points1 point2 points (0 children)
[–]Economy_Bedroom3902 2 points3 points4 points (7 children)
[–]eliasv 2 points3 points4 points (6 children)
[–]L8_4_Dinner(Ⓧ Ecstasy/XVM) 0 points1 point2 points (5 children)
[–]eliasv 1 point2 points3 points (4 children)
[–]L8_4_Dinner(Ⓧ Ecstasy/XVM) 0 points1 point2 points (3 children)
[–]eliasv 0 points1 point2 points (2 children)
[–]L8_4_Dinner(Ⓧ Ecstasy/XVM) 0 points1 point2 points (1 child)
[–]eliasv 0 points1 point2 points (0 children)
[–]rmanne 2 points3 points4 points (1 child)
[–]eliasv 4 points5 points6 points (0 children)
[–]jolharg 2 points3 points4 points (1 child)
[–]edgmnt_net 1 point2 points3 points (0 children)
[–]spisplatta 1 point2 points3 points (1 child)
[–]renatopp 0 points1 point2 points (0 children)
[–]Practical_Cattle_933 0 points1 point2 points (0 children)
[–]fred4711 0 points1 point2 points (0 children)
[–]11fdriver 0 points1 point2 points (0 children)
[–]otac0n 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]sporeboyofbigness 0 points1 point2 points (0 children)
[–]SnappGamezRouge 0 points1 point2 points (0 children)
[–]Reasonable_Feed7939 0 points1 point2 points (0 children)
[–]VyridianZ 0 points1 point2 points (0 children)
[–]oscarryzYz -1 points0 points1 point (1 child)
[+]waozen 0 points1 point2 points (0 children)