gpt4 book ai didi

ada - Ada 2012 中的自定义条件失败消息

转载 作者:行者123 更新时间:2023-12-02 19:23:31 25 4
gpt4 key购买 nike

有没有办法为前置条件和后置条件指定自定义错误/失败消息,与谓词的 Predicate_Failure 类比?我似乎无法在官方文档中找到任何内容。 TIA。

最佳答案

您可以使用 raise 表达式(例如 here ),如下例所示。

ma​​in.adb

pragma Assertion_Policy (Check);

with Ada.Text_IO;
with Ada.Float_Text_IO;

procedure Main is

package TIO renames Ada.Text_IO;
package FIO renames Ada.Float_Text_IO;


function Reciprocal (X : Float) return Float is (1.0 / X)
with Pre => (X /= 0.0 or else
raise Constraint_Error with "X must not be 0.");

begin
FIO.Put (Reciprocal (2.0));
TIO.New_Line;

FIO.Put (Reciprocal (0.0));
TIO.New_Line;
end Main;

输出

$ ./obj/main
5.00000E-01

raised CONSTRAINT_ERROR : X must not be 0.
[2020-07-03 22:20:25] process exited with status 1, elapsed time: 00.32s

关于ada - Ada 2012 中的自定义条件失败消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62721622/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com