# Specifying exception raised at pre/postcondition failures

**URL:** <https://forum.ada-lang.io/t/specifying-exception-raised-at-pre-postcondition-failures/1010>\
**Category:** General\
**Created:** [July 23, 2024, 11:33am UTC](https://forum.ada-lang.io/t/specifying-exception-raised-at-pre-postcondition-failures/1010 "2024-07-23T11:33:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![evanescente-ondine](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/evanescente-ondine/32/451_2.png) [@evanescente-ondine](https://forum.ada-lang.io/u/evanescente-ondine)\
**Post date:** [July 23, 2024, 11:33am UTC](https://forum.ada-lang.io/t/specifying-exception-raised-at-pre-postcondition-failures/1010/1 "2024-07-23T11:33:20Z")

</div>

Hi, how do you specify the exception raised by a pre/post-condition failures, as well as other contracts ?  
It seems to me it should be useful, but I can’t find it.  
I’m just trying to get into the habit of specifying every logical property I can think of.

---

<div class="post-metadata">

**Author:** ![jere](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/jere/32/87_2.png) [@jere](https://forum.ada-lang.io/u/jere)\
**Post date:** [July 23, 2024, 2:23pm UTC](https://forum.ada-lang.io/t/specifying-exception-raised-at-pre-postcondition-failures/1010/2 "2024-07-23T14:23:27Z")

</div>

I usually do it in the pre/post condition itself:

```Ada
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Assertions; use Ada.Assertions;

procedure jdoodle is
    pragma Assertion_Policy(Check);
    
    Condition : constant Boolean := False;
    Custom_Exception : exception;
    
    procedure Do_Something
        with Pre => 
            (if Condition then 
                True 
             else 
                raise Custom_Exception with "Failed Pre check" );
                        
    Procedure Do_Something is
    begin
        Put_Line("In Do_Something");
    end;
    
begin
    Do_Something;
exception
    when Assertion_Error => Put("Assertion - "); raise;
    when Custom_Exception => Put("Custom - "); raise;
end jdoodle;

```

Output:

```ada
Custom - 
raised JDOODLE.CUSTOM_EXCEPTION : Failed Pre check

```

---

<div class="post-metadata">

**Author:** ![evanescente-ondine](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/evanescente-ondine/32/451_2.png) [@evanescente-ondine](https://forum.ada-lang.io/u/evanescente-ondine)\
**Post date:** [July 23, 2024, 3:01pm UTC](https://forum.ada-lang.io/t/specifying-exception-raised-at-pre-postcondition-failures/1010/3 "2024-07-23T15:01:12Z")

</div>

but if Custom\_Exception is raised, isn’t the precondition still validated though, in the eyes of automated tools ?

---

<div class="post-metadata">

**Author:** ![jere](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/jere/32/87_2.png) [@jere](https://forum.ada-lang.io/u/jere)\
**Post date:** [July 23, 2024, 4:55pm UTC](https://forum.ada-lang.io/t/specifying-exception-raised-at-pre-postcondition-failures/1010/4 "2024-07-23T16:55:18Z")

</div>

I’m not a spark expert but my understanding is the precondition is checked by the solver and would alert you. Should be easy to test.

Plus raising an exception there is valid ada so any tools should properly handle it. If not you should let the tool vendor know.

---

<div class="post-metadata">

**Author:** ![JC001](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/j/e56c9b/32.png) [@JC001](https://forum.ada-lang.io/u/JC001)\
**Post date:** [July 24, 2024, 8:54am UTC](https://forum.ada-lang.io/t/specifying-exception-raised-at-pre-postcondition-failures/1010/5 "2024-07-24T08:54:56Z")

</div>

The normal way:

```ada
procedure P with Pre => C or else raise E;

```

---

<div class="post-metadata">

**Author:** ![evanescente-ondine](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/evanescente-ondine/32/451_2.png) [@evanescente-ondine](https://forum.ada-lang.io/u/evanescente-ondine)\
**Post date:** [September 29, 2024, 6:48pm UTC](https://forum.ada-lang.io/t/specifying-exception-raised-at-pre-postcondition-failures/1010/6 "2024-09-29T18:48:29Z")

</div>

Okay I read specifically that reaching those branches of the expression with raise is considered a failure of the post/precondition, so the tools will try to prove they are never reached.  
This is what I supposed.

---

<div class="post-metadata">

**Author:** ![OneWingedShark](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/onewingedshark/32/305_2.png) [@OneWingedShark](https://forum.ada-lang.io/u/OneWingedShark)\
**Post date:** [September 29, 2024, 7:23pm UTC](https://forum.ada-lang.io/t/specifying-exception-raised-at-pre-postcondition-failures/1010/7 "2024-09-29T19:23:36Z")

</div>

> [@evanescente-ondine](#):
>
> Hi, how do you specify the exception raised by a pre/post-condition failures, as well as other contracts ?

The item you’re probably looking for is the `Predicate_Failure` aspect.

```ada
Type Serial_Number is new String(1..7)
  with Dynamic_Predicate => (for all C of Serial_Number => C in '0'..'9'),
       Predicate_Failure => raise Constraint_Error 
         with "Invalid character in string: '" & String(Serial_Number) & "'.;

```

---

<div class="post-metadata">

**Author:** ![evanescente-ondine](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/evanescente-ondine/32/451_2.png) [@evanescente-ondine](https://forum.ada-lang.io/u/evanescente-ondine)\
**Post date:** [September 29, 2024, 8:58pm UTC](https://forum.ada-lang.io/t/specifying-exception-raised-at-pre-postcondition-failures/1010/8 "2024-09-29T20:58:28Z")

</div>

it is specific to subtype predicates though.
