Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 520519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:09:13+00:00 2026-05-13T08:09:13+00:00

Say I have some function fn1() in Erlang which returns {ok, Result} if the

  • 0

Say I have some function fn1() in Erlang which returns {ok, Result} if the function was executed successfully and {error, "ErrorReason"} if there was an error.

Now in another function fn2() I call fn1() and I need to check the result of fn1 and proceed only if it is {ok, Result}.

I figured, I can do this using either case or try catch. But Efficiency is my main concern and I’d like to know which of the two methods below is more efficient:

try-catch Method

fn2() ->
   try
      {ok, Result} = fn1(),
      %Do something with Result
      ok
   catch
      throw:Term -> Term;
      exit:Reason -> {exit, Reason};
      error:Reason -> {error,{Reason,erlang:get_stacktrace()}}
   end.

case Method

fn2() ->
   Res = fn1(),
   case Res of
      {ok, Result} -> 
         %Do something with Result
         ok;
      {error, Reason} ->
         Reason
   end.
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:09:14+00:00Added an answer on May 13, 2026 at 8:09 am

    The case method will be more efficient, as it simply pattern matches, and does not involve building a call stack and stuff.

    In both examples you are about to handle the “error” locally, so there is no point in the try catch.What you might see sometimes is something like:

    fn2() ->
      {ok, Result} = fn1(),
      %Do stuff with Result
      ok.
    

    Here the intention is that you make fn2() throw a badmatch, if fn1() did not return ok. You let someone else “above” handle the problem. E.g. this might kill your process, and make your supervisor create a new one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say, I have some function f[a_,b_,c_]=a+b+c . Now I need to define another function,
Say I have some code like this which is called on $(document).ready() $(.someClass).click(function(){ //do
Let's say I have some simple Javascript like: <script> var hello = function(){ alert(Hello
I have some function, int somefunction( //parameters here, let's say int x) { return
Say I have some context where variables are set and a λ-function is called
Let's say I have some HTML and Javascript which adds text fields dynamically to
Say I have some known values, against which I want to create a hash
Let's say I have some JavaScript that looks like this: function A() { var
Say I have some code like this function Chart(start, end, controller, method, chart) {
Let's say I have some function import_data() and inside of this function I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.