Erlang (programming language)/Tutorials/Errors: Difference between revisions
imported>Eric Evers (New page: =Errors= We can deal with errors with throw and catch. In this example the value of an argument causes an error which causes an exception to be thrown. The function g() is only happy when...) |
imported>Tom Morris m (Erlang programming language/Tutorials/Errors moved to Erlang (programming language)/Tutorials/Errors) |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
{{subpages}} | |||
=Errors= | =Errors= | ||
==Working with exceptions== | |||
An exception is a high level error in many programming languages. An | |||
exception allows errors to be caught and processed. An error causes an exception to be thrown, and afterward the exception may be caught and processed. | |||
In this example the value of an argument causes an error which causes an exception to be thrown. The function g() is only happy when the argument is greater then 12. If the arguemnt is less than 13 then an exception is thrown. We try to call g() in start(). If we run into trouble then the exception is caught in the "case catch" structure inside of start(). | |||
Sample program listing: | Sample program listing: |
Latest revision as of 06:07, 8 August 2009
The metadata subpage is missing. You can start it via filling in this form or by following the instructions that come up after clicking on the [show] link to the right. | |||
---|---|---|---|
|
Errors
Working with exceptions
An exception is a high level error in many programming languages. An exception allows errors to be caught and processed. An error causes an exception to be thrown, and afterward the exception may be caught and processed.
In this example the value of an argument causes an error which causes an exception to be thrown. The function g() is only happy when the argument is greater then 12. If the arguemnt is less than 13 then an exception is thrown. We try to call g() in start(). If we run into trouble then the exception is caught in the "case catch" structure inside of start().
Sample program listing:
-module(catch_it). -compile(export_all). % % An example of throw and catch % g(X) when X >= 13 -> ok; g(X) when X < 13 -> throw({exception1, bad_number}). % % Throw in g/1 % Catch in start/1 % start(Input) -> case catch g(Input) of {exception1, Why} -> io:format("trouble is ~w ", [ Why ]); NormalReturnValue -> io:format("good input ~w ", [ NormalReturnValue ] ) end. % %============================================================== >% % sample output: % 8> c(catch_it). {ok,catch_it} % 9> catch_it:start(12). trouble is bad_number ok % 10> catch_it:start(13). good input ok ok