Exceptions.


%%% Raising (throwing) an exception 
declare
fun {Divide N M}
   if M \= 0
   then N div M
   else raise divByZero end
   end
end

{Browse {Divide 5 2}}
%{Browse {Divide 5 0}}
%%% Catching an exception:
try
   {Browse {Divide 5 0}}
catch divByZero then
   {Browse 'Division by zero is bad'}
end

%%% An exception can have a parameter
declare
fun {YearsUntilGraduation GradYear}
   if (GradYear < 2016 orelse GradYear > 2021)
   then raise invalidGradYear(GradYear) end
   else GradYear - 2016 end
end

{Browse {YearsUntilGraduation 2019}}

%%% The parameter can be used when the exception is caught
try
   {Browse {YearsUntilGraduation 2040}}
catch invalidGradYear(Year) then 
   {Browse 'The year '#Year#' is invalid'}
end
  

CSci 4651 course web site.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.