undefined try symbol

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raghavisayee
    New Member
    • Sep 2013
    • 9

    undefined try symbol

    What should I do if ma compiler says undefined symbol try during exception handling??
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Probably you should show us the code. You could also check this old thread where someone had the same problem. It might help.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      I remember doing something like that. In my case I used a variable from inside the catch block that was defined in the try block. The try block is out of scope when the catch block starts so the variable was undefined. All I had to do was define the variable outside the try block and everything worked.

      Comment

      • raghavisayee
        New Member
        • Sep 2013
        • 9

        #4
        Code:
        #include<iostream.h>
        #include<conio.h>
        void check(int ,int,int);
        class e1{};
        class day
        {
        	private:
        	int date,month,year;
        	public:
        	void getdata()
        	{
        		cout<<"Enter date,month,year\n";
        		cin>>date>>month>>year;
        	}
        	void display()
        	{
        		cout<<date<<"/"<<month<<"/"<<year;
        	}
        	void get()
        	{
        		check(date,month,year);
        	}
        };
        void main()
        {
        	day d;
        	d.getdata();
        	try
        	{
        		void check(int d1,int m1,int y1)
        		{
        			if(d1>31||m1>12||y1>13)
        			{
        				throw e1;
        			}
        		}
        	}
        	catch(e1)
        	{
        		cout<<"Invalid";
        	}
        	d.display();
        	getch();
        }
        Last edited by Banfa; Sep 26 '13, 08:27 AM. Reason: Added code tags [code] ... [/code] round the code

        Comment

        • Nepomuk
          Recognized Expert Specialist
          • Aug 2007
          • 3111

          #5
          There are a few things wrong with that code, so let's go through them one by one:
          • If you want to use cin and cout without adding the namespace to every call (which would be std::cin and std::cout) you'll have to use that namespace by adding
            Code:
            using namespace std;
            after your imports
          • You define a function inside the try block. That is not possible in C/C++; but as you have a prototype at the top, you can add it right at the bottom of the code.
          • Instead of defining the check-function in that catch block you probably want to call
            Code:
            d.get();
          • You can't throw a class but you can throw an instance of a class. So instead of
            Code:
            throw e1;
            you probably want
            Code:
            throw new e1;

          Btw, please use [CODE] tags when posting code. It makes it much easier to read.

          Comment

          • raghavisayee
            New Member
            • Sep 2013
            • 9

            #6
            if i want to code this program in linx environment how should i compile and run this???

            Comment

            • raghavisayee
              New Member
              • Sep 2013
              • 9

              #7
              thanks alot....I got the output...

              Comment

              • Nepomuk
                Recognized Expert Specialist
                • Aug 2007
                • 3111

                #8
                I assume then, that the question of how to compile and run it is answered?

                Comment

                • raghavisayee
                  New Member
                  • Sep 2013
                  • 9

                  #9
                  yes... i tried and succeeded

                  Comment

                  Working...