Exception handling syntax: Difference between revisions

Content deleted Content added
No edit summary
Line 8:
 
==== Exception declarations ====
<source lang=ada>
 
Some_Error : '''exception''';
</source>
 
==== Raising exceptions ====
<source lang=ada>
raise Some_Error;
 
'''raise''' Some_Error with "Out of memory"; -- specific diagnostic message
</source>
'''raise''' Some_Error '''with''' "Out of memory"; -- specific diagnostic message
 
==== Exception handling and propagation ====
<source lang=ada>
'''with''' Ada.Exceptions, Ada.Text_IO;
 
'''procedure''' Foo '''is'''
'''with''' Ada.Exceptions, Ada.Text_IO;
Some_Error : '''exception''';
'''begin'''
'''procedure''' Foo '''is'''
Do_Something_Interesting;
Some_Error : '''exception''';
'''exception''' ''-- Start of exception handlers''
'''begin'''
when Constraint_Error =>
Do_Something_Interesting;
''... -- Handle constraint error''
'''exception''' ''-- Start of exception handlers''
'''when''' Constraint_ErrorStorage_Error =>
''-- Propagate Storage_Error as a different exception with a useful message''
''... -- Handle constraint error''
''' raise''' Some_Error '''with''' "Out of memory"; -- specific diagnostic message
'''when''' Storage_Error =>
'''when''' Error : '''others''' =>
''-- Propagate Storage_Error as a different exception with a useful message''
''-- Handle all others''
'''raise''' Some_Error '''with''' "Out of memory";
Ada.Text_IO.Put("Exception: ");
'''when''' Error : '''others''' =>
Ada.Text_IO.Put_Line(Ada.Exceptions.Exception_Name(Error));
''-- Handle all others''
Ada.Text_IO.PutPut_Line("Exception: "Ada.Exceptions.Exception_Message(Error));
'''end''' Foo;
Ada.Text_IO.Put_Line(Ada.Exceptions.Exception_Name(Error));
</source>
Ada.Text_IO.Put_Line(Ada.Exceptions.Exception_Message(Error));
'''end''' Foo;
 
=== [[BASIC]] ===