exceptions

This is a discussion on exceptions within the C++ Programming forums, part of the General Programming Boards category; I have enum for errors for instance: Code: enum socket_errors { ERR_UNRESOLVABLE = SOCKET_ERRORS, ERR_INTERNAL, ERR_SOCK_CREATION, ERR_BIND_FAILED }; And when ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    exceptions

    I have enum for errors for instance:

    Code:
    enum socket_errors {
    	ERR_UNRESOLVABLE = SOCKET_ERRORS,
    	ERR_INTERNAL,
    	ERR_SOCK_CREATION,
    	ERR_BIND_FAILED
    };
    And when I want to throw in some function this way:

    Code:
    void prevfunc() {
        //stuff//
        throw ERR_SOCK_CREATION;
        return;
    }
    I cannot catch this way in some other function that is calling previous function:
    Code:
    try {
         prevfunc();
    }
    catch (unsigned int) {
    }
    What would be the right way to catch it? What should I look for?

  2. #2
    Registered User
    Join Date
    Oct 2005
    Posts
    88
    Try looking for socket_errors ?

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Look for the enum, not uint.

    Code:
    int _tmain(int argc, TCHAR ** argv)
    {
    	enum test { OH, MY, GOD };
    
    	try
    	{
    		throw GOD;
    	}
    
    	catch(enum test t)
    	{
    		std::cout << t << std::endl;
    	}
    }

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Throw class instances, they're less ambiguous about their type than primitives and enums.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    In addition: http://www.parashift.com/c++-faq-lite/exceptions.html

    You might want to pay special attention to 17.6 and 17.7
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Are Exceptions effective?
    By Petike in forum C++ Programming
    Replies: 5
    Last Post: 09-13-2008, 12:23 AM
  2. Intercepting Fortran exceptions
    By MarkZWEERS in forum C++ Programming
    Replies: 2
    Last Post: 08-06-2008, 09:13 AM
  3. Debug --> Exceptions in Visual Studio 2005
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 08-10-2007, 02:12 AM
  4. Need advice: catch exceptions or call methods to check bits?
    By registering in forum C++ Programming
    Replies: 1
    Last Post: 10-03-2003, 01:49 PM
  5. Throwing exceptions with constructors
    By nickname_changed in forum C++ Programming
    Replies: 14
    Last Post: 07-08-2003, 09:21 AM

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