Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MYAPI_ERROR getObjectSize(MYAPIHandle h, int* returnedSize);
- int getObjectSize(MYAPIHandle h, MYAPI_ERROR* returnedError);
- int size;
- if(getObjectSize(h, &size) != MYAPI_SUCCESS) {
- // Error handling
- }
- MYAPIError error;
- int size;
- size = getObjectSize(h, &error);
- if(error != MYAPI_SUCCESS) {
- // Error handling
- }
- rc = func(..., int **return_array, size_t *array_length);
- if ((rc = func(...)) != API_SUCCESS) {
- /* Error Handling */
- }
- /* Check for valid arguments */
- if (NULL == return_array || NULL == array_length)
- return API_INVALID_ARGS;
- #include <setjmp.h>
- #include <stdio.h>
- jmp_buf x;
- void f()
- {
- longjmp(x,5); // throw 5;
- }
- int main()
- {
- // output of this program is 5.
- int i = 0;
- if ( (i = setjmp(x)) == 0 )// try{
- {
- f();
- } // } --> end of try{
- else // catch(i){
- {
- switch( i )
- {
- case 1:
- case 2:
- default: fprintf( stdout, "error code = %dn", i); break;
- }
- } // } --> end of catch(i){
- return 0;
- }
- #include <stdio.h>
- #include <setjmp.h>
- #define TRY do{ jmp_buf ex_buf__; if( !setjmp(ex_buf__) ){
- #define CATCH } else {
- #define ETRY } }while(0)
- #define THROW longjmp(ex_buf__, 1)
- int
- main(int argc, char** argv)
- {
- TRY
- {
- printf("In Try Statementn");
- THROW;
- printf("I do not appearn");
- }
- CATCH
- {
- printf("Got Exception!n");
- }
- ETRY;
- return 0;
- }
- NSError *error = nil;
- if ([myThing doThingError: &error] == NO)
- {
- // error handling
- }
- if (MyFunc())
- DoSomething();
- int size;
- if(getObjectSize(h, &size) != MYAPI_SUCCESS) {
- // Error handling
- }
- int size;
- MYAPIError rc;
- rc = getObjectSize(h, &size)
- if ( rc != MYAPI_SUCCESS) {
- // Error handling
- }
- int getObjectSize(MYAPIHandle h, int* returnedSize);
- MYAPI_ERROR LastError;
- MYAPI_ERROR* getLastError() {return LastError;};
- #define FUNC_SUCCESS 1
- #define FUNC_FAIL 0
- if(getObjectSize(h, &size) != FUNC_SUCCESS ) {
- MYAPI_ERROR* error = getLastError();
- // error handling
- }
- struct lnode *insert(char *data, int len, struct lnode *list) {
- struct lnode *p, *q;
- uint8_t good;
- struct {
- uint8_t alloc_node : 1;
- uint8_t alloc_str : 1;
- } cleanup = { 0, 0 };
- // allocate node.
- p = (struct lnode *)malloc(sizeof(struct lnode));
- good = cleanup.alloc_node = (p != NULL);
- // good? then allocate str
- if (good) {
- p->str = (char *)malloc(sizeof(char)*len);
- good = cleanup.alloc_str = (p->str != NULL);
- }
- // good? copy data
- if(good) {
- memcpy ( p->str, data, len );
- }
- // still good? insert in list
- if(good) {
- if(NULL == list) {
- p->next = NULL;
- list = p;
- } else {
- q = list;
- while(q->next != NULL && good) {
- // duplicate found--not good
- good = (strcmp(q->str,p->str) != 0);
- q = q->next;
- }
- if (good) {
- p->next = q->next;
- q->next = p;
- }
- }
- }
- // not-good? cleanup.
- if(!good) {
- if(cleanup.alloc_str) free(p->str);
- if(cleanup.alloc_node) free(p);
- }
- // good? return list or else return NULL
- return (good? list: NULL);
- MyHandle * h = MyApiCreateHandle();
- /* first call checks for pointer nullity, since we cannot retrieve error code
- on a NULL pointer */
- if (h == NULL)
- return 0;
- /* from here h is a valid handle */
- /* get a pointer to the error struct that will be updated with each call */
- MyApiError * err = MyApiGetError(h);
- MyApiFileDescriptor * fd = MyApiOpenFile("/path/to/file.ext");
- /* we want to know what can go wrong */
- if (err->code != MyApi_ERROR_OK) {
- fprintf(stderr, "(%d) %sn", err->code, err->message);
- MyApiDestroy(h);
- return 0;
- }
- MyApiRecord record;
- /* here the API could refuse to execute the operation if the previous one
- yielded an error, and eventually close the file descriptor itself if
- the error is not recoverable */
- MyApiReadFileRecord(h, &record, sizeof(record));
- /* we want to know what can go wrong, here using a macro checking for failure */
- if (MyApi_FAILED(err)) {
- fprintf(stderr, "(%d) %sn", err->code, err->message);
- MyApiDestroy(h);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement