ֱ�����ڣ�������ʾ�����еĴ���Ƭ���У��� CORBA_exception_init ���õĸ�������һЩ CORBA_Environment ����... �����ǿ�һ����Щ�����Ǹ�ʲô�õİɡ�
����ûʲô�µĶ���: C ���Բ�֧�������������뽨��һ��������ϵͳ��֧���źź����ƵĶ���(��һ�� GTK+ �ź�ϵͳ)��CORBA ��������һ��ϵͳ������ʹ���������ġ� ÿ����������һ�� CORBA_Environment * �������ڴ洢��������ݡ������һ������ִ���ڼ�����(raise)�����⣬���ⷢ������ʵ���������ݽ����洢������ṹ�С��������һ����
typedef struct CORBA_Environment { CORBA_exception_type _major; /* .... */ } CORBA_Environment; |
_major �ֶ�ָʾ�ڷ��������ڼ��Ƿ�������һ�����⡣�������� 3 ��ֵ: CORBA_NO_EXCEPTION�� CORBA_USER_EXCEPTION �� CORBA_SYSTEM_EXCEPTION�����ǵ���˼������� ���͵ģ����õĴ���Ӧ����ÿ����������֮������Ƿ���������(��Ϊ�쳣����Щ)��
CORBA_Environment ev; if (ev->_major == CORBA_NO_EXCEPTION) /* goooood :) */ else /* baaaaad :( */ |
������һϵ�к���������Щ�����ϵIJ��������ǿ����� CORBA_exception_set ����(throw)���⡣CORBA_exception_id ������������ı�ʶ�����һ���ַ�����CORBA_exception_value ����һ��������ṹ��ָ�룬CORBA_exception_free ������һ������֮�����³�ʼ��һ�� ev �ṹ�����������ǵIJ���˵����
extern CORBA_char *CORBA_exception_id (CORBA_Environment *ev); extern void *CORBA_exception_value (CORBA_Environment *ev); extern void CORBA_exception_free (CORBA_Environment *ev); extern void CORBA_exception_set (CORBA_Environment *ev, CORBA_exception_type major, CORBA_char *exception_id, void *parameters); |
������ʹ����ǰ3��������һ���������룺
// ��������������ʵĽӿ� module FruitsBasket { interface Apple { exception eaten { string<40> why; }; void EatMe () raises (eaten); }; }; |
#include <orb/orbit.h> /* �����Ƿ��ʽӿڵĴ��� */ int main (int argc, char *argv) { CORBA_Object obj; CORBA_Environment ev; CORBA_ORB orb; CORBA_exception_init (&ev); orb = CORBA_ORB_init (&ev); if (ev->_major != CORBA_NO_EXCEPTION) { fprintf (stderr, "Error: unable to initialize the ORB: %s\n", CORBA_exception_id (&ev)); CORBA_exception_free (&ev); exit (1); } /* ���� Apple ����Ĵ��� */ /* ע��ʹ�� switch /case �����ܸ�����һЩ * ���������������Щ���� ... */ FruitsBasket_Apple_EatMe (orb, &ev); if (ev->_major != CORBA_NO_EXCEPTION) { if (ev->_major == CORBA_USER_EXCEPTION) { CORBA_char *buffer; buffer = CORBA_exception_id (&ev); if (strcmp (buffer, "FruitsBasket_Apple_eaten")) { fprintf (stderr, "user exception: Apple already eaten: %s\n", (CORBA_exception_value (&ev))->why); exit (1); } else { fprintf (stderr, "unknown exception raised!! Error!!\n"); exit (2); } } else { fprintf (stderr, "System exception: %s\n", CORBA_exception_id (&ev)); exit (1); } } return 0; } |
���������Ĵ������������⣬��������ȷ�IJ������ݸ� CORBA_exception_set�� CORBA_exception_type ������ CORBA_USER_EXCEPTION �� CORBA_SYSTEM_EXCEPTION ����֮һ�� CORBA_char ��������ͷ�ļ��� #define ����������֣� void * ��һ��������ṹ��ָ�룬����ṹ���ֶ��Ѿ�����䣬����ṹ���ڴ����� __alloc() ����ġ�������ʹ�� Apple �����һ��С���ӣ�
/* �����ڷ������в���������Ҫ���� "eaten" ���� * ��������ȷ��ԭ��"why" * ע�����ǵ� Apple �������⽫��ӳ�����������ݽṹ�� * / typedef struct FruitsBasket_Apple_eaten { CORBA_char why[40]; /* ... ���ϸ��ָ������ض���ʵ�ֵij�Ա */ } FruitsBasket_Apple_eaten; #define ex_FruitsBasket_Apple_eaten <identifier> FruitsBasket_Apple_eaten *FruitsBasket_Apple_eatme__alloc () */ FruitsBasket_Apple_eaten *exception; exception = FruitsBasket_Apple_eaten__alloc (); strcpy (exception->why, "mmmm..."); CORBA_exception_set (&ev, CORBA_USER_EXCEPTION, ex_FruitsBasket_Apple_eaten, (void *) exception); |
����������������Ѿ�û��ʲô�����ˣ�����Ժ����IJ鿴 hello world �Ĵ��롣����ʹ������Ľӿڶ��壺
interface Echo { void echo (in string<40> str); }; |
������Դ����:
main () { CORBA_ORB orb; CORBA_Environment ev; CORBA_Object obj; FILE *file; /* һ���dz������ַ���: �������������е���Ҫ :) */ char buffer[10000]; char *ior; CORBA_exception_init(&ev); orb = CORBA_ORB_init(&argc, argv, "orbit-local-orb", &ev); if (ev->_major != CORBA_NO_EXCEPTION) { fprintf (stderr, "Error: exception during ORB init: %s\n", CORBA_exception_id(&ev)); exit (1); } if (!file = fopen ("object.ior", "r")) { fprintf (stderr, "error: could not open serializing file\n"); exit (2); } fgets (buffer, 10000, file); ior = g_strdup (buffer); /* �õ�ԭʼ���ַ��� */ /* ����, ��������ӵ������ !! */ obj = CORBA_ORB_string_to_object (orb, ior, &ev); if (ev->_major != CORBA_NO_EXCEPTION) { fprintf (stderr, "Error: could not get object reference: %s\n", CORBA_exception_id(&ev)); exit (3); } fclose (file); Echo_echo (obj, ,&ev); if (ev->_major != CORBA_NO_EXCEPTION) fprintf (stderr, "Error: could not get method: %s\n", CORBA_exception_id(&ev)); CORBA_exception_free(&ev); /* ����, ���ǿ��Ե�����������... */ } |
��ʵ�ϣ���Щ��������ǰ�����������ģ�ֻ���������������