Exceptions/Catch an exception thrown in a nested call: Difference between revisions

From Rosetta Code
Content added Content deleted
m (Grammar)
(Ada)
Line 7: Line 7:
# Function foo should catch only exception U0, not U1.
# Function foo should catch only exception U0, not U1.
Show/describe what happens when the program is run.
Show/describe what happens when the program is run.

=={{header|Ada}}==
<lang ada>with Ada.Text_Io; use Ada.Text_Io;

procedure Exceptions_From_Nested_Calls is
U0 : exception;
U1 : exception;
Baz_Count : Natural := 0;
procedure Baz is
begin
Baz_Count := Baz_Count + 1;
if Baz_Count = 1 then
raise U0;
else
raise U1;
end if;
end Baz;
procedure Bar is
begin
Baz;
end Bar;
procedure Foo is
begin
Bar;
exception
when U0 =>
Put_Line("Procedure Foo caught exception U0");
end Foo;
begin
for I in 1..2 loop
Foo;
end loop;
exception
when U1 =>
Put_line("Exception U1 passed through foo");
end Exceptions_From_Nested_Calls;</lang>
Sample output:
<pre>
Procedure Foo caught exception U0
Exception U1 passed through foo</pre>


=={{header|OCaml}}==
=={{header|OCaml}}==

Revision as of 03:00, 15 March 2009

Task
Exceptions/Catch an exception thrown in a nested call
You are encouraged to solve this task according to the task description, using any language you may know.

Show how to create a user-defined exception and show how to catch an exception raised from several nested calls away.

  1. Create two user-defined exceptions, U0 and U1.
  2. Have function foo call function bar which then calls function baz.
  3. Arrange for function baz to raise, or throw exception U0 on its first call, then exception U1 on its second.
  4. Function foo should catch only exception U0, not U1.

Show/describe what happens when the program is run.

Ada

<lang ada>with Ada.Text_Io; use Ada.Text_Io;

procedure Exceptions_From_Nested_Calls is

  U0 : exception;
  U1 : exception;
  Baz_Count : Natural := 0;
  procedure Baz is
  begin
     Baz_Count := Baz_Count + 1;
     if Baz_Count = 1 then
        raise U0;
     else
        raise U1;
     end if;
  end Baz;
  procedure Bar is
  begin
     Baz;
  end Bar;
  procedure Foo is
  begin
     Bar;
  exception
     when U0 =>
        Put_Line("Procedure Foo caught exception U0");
  end Foo;

begin

  for I in 1..2 loop
     Foo;
  end loop;

exception

  when U1 =>
     Put_line("Exception U1 passed through foo");

end Exceptions_From_Nested_Calls;</lang> Sample output:

Procedure Foo caught exception U0
Exception U1 passed through foo

OCaml

<lang ocaml>exception U0 exception U1

let baz i =

 raise (if i = 0 then U0 else U1)

let bar i = baz i (* Nest those calls *)

let foo () =

 for i = 0 to 1 do
   try
     bar i
   with U0 ->
     print_endline "Function foo caught exception U0"
 done

let () = foo ()</lang> Sample output:

Function foo caught exception U0
Exception: U1.

Python

There is no extra syntax to add to functions and/or methods such as bar, to say what exceptions they may raise or pass through them: <lang python>class U0(Exception): pass class U1(Exception): pass

def foo():

   for i in range(2):
       try:
           bar(i)
       except U0:
           print "Function foo caught exception U0"

def bar(i):

   baz(i) # Nest those calls

def baz(i):

   raise U1 if i else U0

foo()</lang> Sample output:

Function foo caught exception U0

Traceback (most recent call last):
  File "C:/Paddy3118/Exceptions_Through_Nested_Calls.py", line 17, in <module>
    foo()
  File "C:/Paddy3118/Exceptions_Through_Nested_Calls.py", line 7, in foo
    bar(i)
  File "C:/Paddy3118/Exceptions_Through_Nested_Calls.py", line 12, in bar
    baz(i) # Nest those calls
  File "C:/Paddy3118/Exceptions_Through_Nested_Calls.py", line 15, in baz
    raise U1 if i else U0
U1

The first line of the output is generated from catching the U0 exception in function foo.

Uncaught exceptions give information showing where the exception originated through the nested function calls together with the name of the uncaught exception, (U1) to stderr, then quit the running program.