programming erlang ch4 - andstudy/forge GitHub Wiki

μ˜ˆμ™Έ λ°œμƒ μ‹œν‚€κΈ°

μ‘΄μž¬ν•˜μ§€ μ•ŠλŠ” ν•¨μˆ˜ 호좜

    8> shop:cost(milk3).
    
    =ERROR REPORT==== 11-Jul-2008::08:34:10 ===
    Error in process <0.32.0> with exit value: {function_clause,[{shop,cost,[milk3]}
    ,{erl_eval,do_apply,5},{shell,exprs,6},{shell,eval_loop,3}]}
    
    ** exited: {function_clause,[{shop,cost,[milk3]},
                                 {erl_eval,do_apply,5},
                                 {shell,exprs,6},
                                 {shell,eval_loop,3}]} **

직접 λ°œμƒμ‹œν‚€κΈ°

    generate_exception(1) -> a;
    generate_exception(2) -> throw(a); %% μ˜ˆμ™Έμƒμ„±λ¨
    generate_exception(3) -> exit(a); %%  μ˜ˆμ™Έ 생성됨 
    generate_exception(4) -> {'EXIT', a}; 
    generate_exception(5) -> erlang:error(a). %% μ˜ˆμ™Έ 생성됨

μ˜ˆμ™Έλ₯Ό λ°›μ•„λ³΄μž

κΈ°λ³Έν˜•

  • μ˜ˆμ™Έ 처리 μ½”λ“œ

      demo1() ->
           [catcher(I) || I <- [1,2,3,4,5]].
      catcher(N) ->
         try generate_exception(N) of
             Val -> {N, normal, Val}
         catch
             throw:X -> {N, caught, thrown, X};
             exit:X  -> {N, caught, exited, X};
             error:X -> {N, caught, error, X}
         end.
    
  • μ‹€ν–‰ κ²°κ³Ό

      9> try_test:demo1().
      [{1,normal,a},
       {2,caught,thrown,a},
       {3,caught,exited,a},
       {4,normal,{'EXIT',a}},
       {5,caught,error,a}]
    

catch κΈ°λ³Έλͺ…령을 μ΄μš©ν•˜μ—¬ ν•œλ²ˆμ— λ°›κΈ°

  • μ†ŒμŠ€μ½”λ“œ

      demo2() ->
          [{I, (catch generate_exception(I))} || I <- [1,2,3,4,5]].
    
  • μ‹€ν–‰κ²°κ³Ό

      10> try_test:demo2().
      [{1,a},
       {2,a},
       {3,{'EXIT',a}},
       {4,{'EXIT',a}},
       {5,
        {'EXIT',{a,[{try_test,generate_exception,1},
                    {try_test,'-demo2/0-lc$^0/1-0-',1},
                    {try_test,'-demo2/0-lc$^0/1-0-',1},
                    {erl_eval,do_apply,5},
                    {shell,exprs,6},
                    {shell,eval_loop,3}] } } }]
    

κ°€λŠ₯ν•œ λͺ¨λ“  μ˜ˆμ™Έλ₯Ό 작기

    try Expr
    catch
       _:_ -> ... λͺ¨λ“  μ˜ˆμ™Έλ₯Ό μ²˜λ¦¬ν•˜λŠ” μ½”λ“œ ...
    end

μ˜ˆμ™Έ λ°œμƒμ‹œ μŠ€ν…μ„ μ°μ–΄λ³΄μž

  • μ†ŒμŠ€ μ½”λ“œ

      demo3() ->
          try generate_exception(5) 
          catch
             error:X ->
      	    {X, erlang:get_stacktrace()}
          end.
    
  • μ‹€ν–‰ κ²°κ³Ό

      11> try_test:demo3().
      {a,[{try_test,generate_exception,1},
          {try_test,demo3,0},
          {erl_eval,do_apply,5},
          {shell,exprs,6},
          {shell,eval_loop,3}]}
    

try .. catch ν”„λ‘œκ·Έλž˜λ° μŠ€νƒ€μΌ

ν”νžˆ 였λ₯˜κ°€ λ°˜ν™˜ λ˜λŠ” μ½”λ“œ

      case f(X) of 
           {ok,Val} -> do_some_thing_with(Val);
           {error,Why} -> %% 였λ₯˜λ‘œ 무엇인가λ₯Ό ν•œλ‹€.
      end,

였λ₯˜κ°€ λ‚  μˆ˜λŠ” μžˆμœΌλ‚˜ λ“œλ¬Έ 경우의 μ½”λ“œ

  • μ‚¬μš©λ²•

        try my_func(X)
        catch
           throw:{thisError,X} -> ..
           throws:{someOhterError,X) -> ...
        end
    
  • μ •μ˜

         my_func(X) ->
              case ... of
                   ... -> .. throw({thisError,...)}
                   ... -> .. throw({someOhterError, ...})
    
⚠️ **GitHub.com Fallback** ⚠️