Skip to content

Instantly share code, notes, and snippets.

@zaphar
Created April 11, 2009 14:20
Show Gist options
  • Save zaphar/93574 to your computer and use it in GitHub Desktop.
Save zaphar/93574 to your computer and use it in GitHub Desktop.
function return_errors(DoError) ->
case DoError of
true ->
{error, true};
false ->
ok
end
.
%% this function will always throw an exception
%% because I didn't handle the error return: {error, true}
function process_errors_badly() ->
case return_errors(true) of
ok ->
io:format("It worked!!", []);
err ->
io:format("Why don't I ever execute?", []);
end
.
%% because I did handle the error case
%% this code executes without an exception
process_error_correctly() ->
case return_errors(true) of
ok ->
io:format("It worked!!", []);
{error, true} ->
io:format("Yay I caught the error", []);
end
.
sub return_errors() {
my $do_error = shift;
if ($do_error) {
return {error => 1};
}
return {error => 0};
}
my $is_error = return_errors(1);
# I'm not correctly handling the error so this will never execute.
# Don't worry though something else somewhere seemingly completely
# unrelated will fail so I'll find out after a week of debugging.
if ($is_error->{err}) {
die "there is an error!!!";
}
die "Oh no how did I get here?";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment