IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

Erlang程序设计,第四章 异常

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

第四章 异常

Table of Contents

  • 第四章 异常
    • 4.1 异常
    • 4.2 抛出异常
    • 4.3 try…catch
      • 4.3.1 缩减版本
      • 4.3.2 使用try…catch的编程惯例
    • 4.4 catch
    • 4.5 改进错误信息
    • 4.6 try…catch的编程风格
      • 4.6.1 经常会返回错误的程序
      • 4.6.2 出错几率比较小的程序
    • 4.7 捕获所有可能的异常
    • 4.8 新老两种异常处理风格
    • 4.9 栈跟踪

第四章 异常

4.1 异常

Erlang通过throw(Exception)、exit(Exception)、erlang:error(Exception)来抛出异常。
Erlang捕获异常的两种方式:

1、 使用try…catch表达式将函数括起来(同java)
2、 把函数调用包含在catch表达式中

4.2 抛出异常

exit(Why)

显式的产生错误

throw(Why)

抛出调用者可能会捕获的异常(同java)

erlang:error(Why)

系统级错误

4.3 try…catch

try…catch的语法结构:

%% 首先对FuncOrExpressionSequence求值
%% 如果没有产生异常则顺序进行Patterm匹配, 匹配成功后执行后面的表达式
%% 如果有异常抛出, 则顺序匹配ExPattern(ExceptionType是throw、exit、error中的一个, 默认为throw)
%% after块中的代码用于清理工作, 同java异常捕获时的finally 
try FuncOrExpressionSequence of
    Pattern1 [when Guard1] ->Expressions1;
    Pattern2 [when Guard2] ->Expressions2;
    ...
catch
    ExceptionType: ExPattern1 [when ExGuard1] ->ExExpressions1;
    ExceptionType: ExPattern2 [when ExGuard2] ->ExExpressions2;
    ...
after
    AfterExpressions
end.

其相当于在尾部带有catch和after块的case表达式。

4.3.1 缩减版本

%% 省略掉after块
try F
catch
    ...
end.

4.3.2 使用try…catch的编程惯例

%% 将三种异常方式依次捕获
try F
catch
    throw: X ->ExExpressions1;
    exit: X  ->ExExpressions2;
    error: X ->ExExpressions3
end.

4.4 catch

使用catch原语捕获异常, 返回的描述错误的元组可以提供更详细的信息。

4.5 改进错误信息

使用erlang:error处理函数的异常参数, 可以获取更清晰的错误信息。

4.6 try…catch的编程风格

4.6.1 经常会返回错误的程序

%% 可以针对两种可能的返回值分别处理 
case f(X) of
    {ok, Val}    ->Expressions1;
    {error, Why} ->Expressions2
end.
%% 也可以只处理正常返回, 而对非正常返回抛出异常
{ok, Val} = f(X),
Expressions1;
...

4.6.2 出错几率比较小的程序

检测异常的代码分支应与函数中异常抛出的分支互相匹配。

%% 函数中抛出异常的分支
f(X) ->
    case ... of
        ... ->
            ... throw({thisError, ...})
        ... ->
            ... throw({someOtherError, ...})

%% 检测异常的代码分支
try f(X)
catch
    throw:{thisError, X}      ->...
    throw:{someOtherError, X} ->...
end.

4.7 捕获所有可能的异常

%% 使用通配符'_'来匹配异常类型(throw,exit,error)和异常值 
try Expr
catch
    _:_ ->...
end.

4.8 新老两种异常处理风格

%% 旧的风格
case (catch foo(...)) of
    {'EXIT', Why} ->...
    Val           ->...
end.

%% 新的风格
try foo(...) of
    Val       ->...
catch
    exit: Why ->...
end.

4.9 栈跟踪

erlang:get_stacktrace()可以查看栈跟踪信息, 即如果调用不发生异常那么这些信息中就包括了函数的调用路径。

文章永久链接:https://tech.souyunku.com/?p=23502


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(69) 打赏



未经允许不得转载:搜云库技术团队 » Erlang程序设计,第四章 异常

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367