Problems with exceptions

This is a discussion on Problems with exceptions within the C++ Programming forums, part of the General Programming Boards category; I have a problem with handling exceptions. It is as below: I have a class lets say ABC (in ABC.h): ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    6

    Problems with exceptions

    I have a problem with handling exceptions. It is as below:

    I have a class lets say ABC (in ABC.h):

    Code:
    class ABC {
                  public:
                        ABC(const char *name="ME";
                              ) throw (Ex,Exfile);
                   ABC(const ABC& Y)
                           throw();
                   virtual ~ABC();
                  };
    Next I have the constructors defined as:

    Code:
    ABC::ABC(const char* _name)
                 throw(Ex,Exfile);
    
    ABC::ABC(const ABC& Y):name()
                  {
                 try {Init();
                        Copy(Y);
                       }
               catch(Ex &)
                                {  }
    }

    Further I have Ex structure as:

    Code:
    struct Ex{ int errornu;
                     Ex(int erronu);
                  virtual  ~Ex();
                };
    }


    upon compilation of the program I get the following error:

    Code:
    declaration of `ABC::ABC(const ABC &)' throws different exception.
    ABC.h:19: previous declaration here
    Can any one help in resoving this error?
    Last edited by maneesh; 11-23-2005 at 01:50 PM.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    throw();
    throw() re-throws the current exception and is usually used only in catch blocks. You need to actually throw a type.

    Code:
    class aclass {};
    
    void fuunction(void) {
        throw aclass();
    }
    
    try {
        function();
    }
    catch(aclass e) {
        // error
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Code:
    ABC(const ABC& Y) throw();
    In this case, I believe throw specifies that the function cannot throw an exception, while throw(type) specifies an exception of type can be thrown (correct me if I'm wrong)

    Edit: This looks like a mistake to me:
    Code:
       ABC(const char *name="ME";) throw (Ex,Exfile);
    Last edited by JaWiB; 11-23-2005 at 04:23 PM.
    "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

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by dwks
    Code:
    throw();
    throw() re-throws the current exception and is usually used only in catch blocks. You need to actually throw a type.

    Code:
    class aclass {};
    
    void fuunction(void) {
        throw aclass();
    }
    
    try {
        function();
    }
    catch(aclass e) {
        // error
    }
    This line:
    Code:
    ABC(const ABC& Y)
             throw();
    is a function prototype--not a function definition, and it can be rewritten like this:
    Code:
    ABC(const ABC& Y) throw();
    throw() is an exception specification which enforces which exceptions a function is allowed to throw(). A function can only throw the exceptions listed between the parentheses, and if you don't specify any then the function is not allowed to throw any exceptions. Here are some examples:
    Code:
    #include<iostream>
    using namespace std;
    
    void someFunc() throw(const char*)
    {
    	throw("hello exception");
    }
    
    int main()
    {
    	try
    	{
    		someFunc();
    	}
    	catch(const char* message)
    	{
    		cout<<message<<endl;
    	}
    
    	return 0;
    }
    Code:
    #include<iostream>
    using namespace std;
    
    void someFunc() throw()
    {
    	try
    	{
    		throw("hello exception");
    	}
    	catch(...)
    	{
    		cout<<"exceptions that can't be thrown."<<endl;
    	}
    }
    
    int main()
    {
    	try
    	{
    		someFunc();
    	}
    	
    	catch(const char* message)
    	{
    		cout<<message<<endl;
    	}
    
    	return 0;
    }
    Last edited by 7stud; 11-23-2005 at 05:31 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 03:59 AM
  2. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 01:17 PM
  3. Need advice: catch exceptions or call methods to check bits?
    By registering in forum C++ Programming
    Replies: 1
    Last Post: 10-03-2003, 02:49 PM
  4. Throwing exceptions with constructors
    By nickname_changed in forum C++ Programming
    Replies: 14
    Last Post: 07-08-2003, 10:21 AM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM

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