Skip to content

Instantly share code, notes, and snippets.

@ytomino
Created May 20, 2014 01:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ytomino/3015eb443e956a47e262 to your computer and use it in GitHub Desktop.
Save ytomino/3015eb443e956a47e262 to your computer and use it in GitHub Desktop.
throwing and catching a C++ exception
with Ada.Text_IO;
with Interfaces.C.Strings;
with GNAT.CPP_Exceptions;
procedure cppexc is
pragma Linker_Options ("-lstdc++");
use type Interfaces.C.char_array;
char_const_ptr : exception;
pragma Import (Cpp, char_const_ptr, "_ZTIPKc"); -- typeid(char const *)
begin
Ada.Text_IO.Put_Line ("throw...");
declare
procedure Raise_char_const_ptr is
new GNAT.CPP_Exceptions.Raise_Cpp_Exception (
Interfaces.C.Strings.chars_ptr);
Message : constant Interfaces.C.Strings.chars_ptr :=
Interfaces.C.Strings.To_Chars_Ptr (
new Interfaces.C.char_array'(
"This is a message in char const *"
& Interfaces.C.nul));
begin
Raise_char_const_ptr (
char_const_ptr'Identity,
Message);
end;
exception
when E : char_const_ptr =>
Ada.Text_IO.Put_Line ("caught!");
declare
function Get_char_const_ptr is
new GNAT.CPP_Exceptions.Get_Object (
Interfaces.C.Strings.chars_ptr);
Message : Interfaces.C.Strings.chars_ptr;
begin
Message := Get_char_const_ptr (E);
Ada.Text_IO.Put_Line (
"message: "
& Interfaces.C.Strings.Value (Message));
end;
end cppexc;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment