-- Demonstrates exception handling in a loop -- Loop continues after exception is handled -- Begin-exception-end is similar to a java try-catch with ada.text_io; use ada.text_io; with ada.integer_text_io; use ada.integer_text_io; procedure excep2 is n: Natural; s: Natural := 0; c: Character; begin while not end_of_file loop excep: begin -- A begin-end block can be added anywhere get(n); put_line("n: " & n'img); s := s + n; exception when data_error => put_line("Data error: You must enter a number!"); -- A non-numeric character in input raised the exception. -- The character that caused the exception is still -- in the input, and so we read it: get(c); put_line("c=<" & c & ">"); when constraint_error => -- Negative value in the input put_line("Constraint error: You must enter an non-negative value!"); end excep; -- End begin-end block put_line("Repeat the loop"); end loop; put_line("Sum: " & s'img); end excep2; -- Input: --|1a2-ab3c4-55d6-77 8 9 -- Output: --|n: 1 --|Data error: You must enter a number! --|c= --|n: 2 --|Data error: You must enter a number! --|c= --|Data error: You must enter a number! --|c= --|n: 3 --|Data error: You must enter a number! --|c= --|n: 4 --|Constraint error: You must enter an non-negative value! --|Data error: You must enter a number! --|c= --|n: 6 --|Constraint error: You must enter an non-negative value! --|n: 8 --|n: 9 --|Sum: 33