the Try keyword in visual basic, how so in c?

This is a discussion on the Try keyword in visual basic, how so in c? within the C Programming forums, part of the General Programming Boards category; I'm porting some code of a game I found on the web to C. It was written in Visual Basic ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    4

    the Try keyword in visual basic, how so in c?

    I'm porting some code of a game I found on the web to C. It was written in Visual Basic 2005.

    It goes a little something like..
    Code:
    Try
        Do
         'code block
        Loop Until Not Change
    Catch ex As Exception
        Throw New Exception("invalid move")
    End Try
    what would I do in C...
    Code:
    do {
     /*
      more do, break, etc
     */
    } while (!change);
    I have an idea of what it does, but I have no idea of how to logically do such in C, let alone understand how it affects other routines that call a routine containing said code, etc.

    in another routine of the game, it simply has
    Code:
    '---if possible value is string.Empty, then error---
            If str = String.Empty Then
                Throw New Exception("Invalid Move")
            End If
    Can anyone explain please?
    Last edited by X_e_e_k; 05-30-2007 at 07:10 PM.

  2. #2
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    C does not have built-in exception handling. For that you'll have to look at using Microsoft's Exception Handling API.

  3. #3
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    C++ supports the try-catch construct, so you may want to just use that instead.

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    4
    here's my take on it, but i don't know if i'm right
    Code:
    int ex; //some global variable
    
    int routine1() {
     int reply = routine2();
     if (ex) {
      // handle the exception
     }
    }
    /* would be like this in visual basic
     Try
       reply = routine2()
     Catch ex As Exception
       'handle it
     End Try
    */
    
    int routine2() {
     if (strlen(string) == 0) {
      ex = 1;
     }
    }
    /* would be like this in vb
     If String.Empty Then
      Throw New Exception("blah")
     End If
    */

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    The C standard library uses errno for most major error handling, as well as relying on return values from functions.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    C is more along the lines of Yoda's famous maxim:

    "Do or do not; there is no try."

    Pretty easy to see if a string has no char's in it.

  7. #7
    Registered User
    Join Date
    May 2007
    Posts
    4
    yea that empty string was just an example for something on a bigger scale, i'm starting to get the drift of how it affects the flow of code...

    are you guys thinking about program runtime error exceptions or exceptions created by the code itself to inform other parts of the program what's up

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    You don't really need exceptions in the VB sense in C....

    Code:
    char *szInput = readstring(); /* some user defined function to read a string */
    if(szInput)
    {
        /* It worked. */
    }
    else
    {
        /* Failed */
    }

  9. #9
    Registered User
    Join Date
    May 2007
    Posts
    4
    well, yea... an if statement would suffice, but it's making calls to another subroutine, if something in that subroutine returns a value, but there's a separate error (aside from the return value) that's set somewhere in that routine, the calling routine can find out about it

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Usually that's done via errno in the C standard library.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	FILE *f = fopen("123","r");
    	if(f)
    	{
    		fclose(f);
    	}
    	else
    	{
    		perror("Unable to open file.");
    	}
    	return 0;
    }
    perror() prints an error message associated with the error that errno signifies. For example, this is the output I received:

    Code:
    Unable to open file.: No such file or directory

  11. #11
    Registered User
    Join Date
    May 2007
    Posts
    11
    how about assert(); guyz...

  12. #12
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by kky2k View Post
    how about assert(); guyz...
    No, http://www.acm.uiuc.edu/webmonkeys/b...guide/2.1.html
    Last edited by zacs7; 05-31-2007 at 02:35 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 11-23-2007, 12:13 AM
  2. Run Visual Basic code inside C++?
    By torbjoen in forum Windows Programming
    Replies: 8
    Last Post: 08-01-2002, 12:41 AM
  3. <list>
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 02-24-2002, 04:07 PM
  4. Visual Basic C++
    By gussguss in forum C++ Programming
    Replies: 8
    Last Post: 11-20-2001, 10:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21