Exception handling in Visual Studio 2005

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ahammad
    New Member
    • May 2007
    • 79

    Exception handling in Visual Studio 2005

    When using the try catch block in Visual Studio 2005, we cannot have:

    Code:
    try{
    
    [something]
    
    } catch [B](...)[/B] {
    
    [something]
    
    }
    For some reason, we cannot use the (...) in the catch part for exception handling. Is there a way to do this in Visual Studio 2005 so that the code in the catch part would be run for every exception?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This code:
    Originally posted by ahammad
    try{

    //[something]

    } catch (...) {

    //[something]

    }
    compiles with no errors using Visual Studio.NET 2005.

    Have you enabled exception handling in your project properties??

    Comment

    • Silent1Mezzo
      New Member
      • Feb 2007
      • 208

      #3
      Originally posted by ahammad
      When using the try catch block in Visual Studio 2005, we cannot have:

      Code:
      try{
      
      [something]
      
      } catch [B](...)[/B] {
      
      [something]
      
      }
      For some reason, we cannot use the (...) in the catch part for exception handling. Is there a way to do this in Visual Studio 2005 so that the code in the catch part would be run for every exception?
      Couldn't you just catch the super class ?

      Code:
      try{
      
      [something]
      
      } catch [B](Exception e)[/B] {
      
      [something]
      
      }

      Comment

      • ahammad
        New Member
        • May 2007
        • 79

        #4
        Sure it compiles, but whatever is in the catch block doesn't get executed in my case.

        Code:
        	int nums[1];
        	nums[0] = 5;
        	try
        	{
        		int x = nums[99];
        		int y = 2;
        	}
        	catch (...)
        	{
        		TRACE("Catch\n");
        	}
        The program will handle the error, but the TRACE command does not get executed. It's like the program handles the error but skips the catch block altogether.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          This code:

          Originally posted by ahammad
          int nums[1];
          nums[0] = 5;
          try
          {
          int x = nums[99];
          int y = 2;
          }
          catch (...)
          {
          TRACE("Catch\n" );
          }
          Does not throw an exception. It just corrupts memory.

          Without a throw, there can be no catch.

          Also, when you catch a base class object be certain that you catch it by reference or by pointer. If you don't, and it's really a derived obect, all the derived part will be sliced off. So all you will see in the catch block is the base part of your object.

          [code=cpp]
          try{

          [something]

          } catch (Exception e) { <<<<Exception & or Exception* !!

          [something]

          }
          [/code]

          Comment

          • ahammad
            New Member
            • May 2007
            • 79

            #6
            when I do something like this:

            Code:
            int num[2];
            num[0] = 5;
            num[1] = 6;
            
            int testvalue;
            
            
            try
            {
            	testvalue = num[9];
            }
            catch (...)
            {
            	std::cout << "EXCEPTION caught \n";
            
            }
            
            int x;
            cin >> x;
            I get an error and the console application crashes. Shouldn't the program catch the error?

            Comment

            • RedSon
              Recognized Expert Expert
              • Jan 2007
              • 4980

              #7
              Originally posted by ahammad
              when I do something like this:

              Code:
              int num[2];
              num[0] = 5;
              num[1] = 6;
              
              int testvalue;
              
              
              try
              {
              	testvalue = num[9];
              }
              catch (...)
              {
              	std::cout << "EXCEPTION caught \n";
              
              }
              
              int x;
              cin >> x;
              I get an error and the console application crashes. Shouldn't the program catch the error?
              what is in your catch parens?

              Comment

              • ahammad
                New Member
                • May 2007
                • 79

                #8
                Originally posted by RedSon
                what is in your catch parens?
                Nothing except the three dots, what you see is what you get. It is supposed to mean "handle all exceptions".

                The reason I did it like that was because I was told that this should be perfectly legal, and that it doesn't work with Microsoft compilers, but it does work with other compilers. I am not sure if that is true or not.

                Funny thing is, if I use regular arrays, it doesn't work, but if I use a class like vectors, it does. This code for example, works perfectly, and both catch blocks are executed:


                Code:
                    std::vector<int> v;
                
                    try
                    {
                        v.at(10) = 1;
                    }
                    catch (std::out_of_range)
                    {
                        std::cout << "caught std::out_of_range\n";
                    }
                
                    try
                    {
                        v.at(10) = 1;
                    }
                    catch (...)
                    {
                        std::cout << "EXCEPTION: " << e.what() << "\n";
                    }
                The point is, if it is legal in other C++ compilers, then there has to be a similar command instead of (...)...but why does it work for classes and not arrays?

                Comment

                • plemoine
                  New Member
                  • Jun 2007
                  • 15

                  #9
                  This is normal: C "arrays" are dumb and not protected. An array subscript is simply a pointer, an iterator into memory that you can increment or decrement at will... until you corrupt something else in memory.

                  On the other hand, STL vectors do bound checking and throw an exception from operator[] if you blow the limit. But this is not built-in in the language itself, it is in STL.


                  Originally posted by ahammad
                  Nothing except the three dots, what you see is what you get. It is supposed to mean "handle all exceptions".

                  The reason I did it like that was because I was told that this should be perfectly legal, and that it doesn't work with Microsoft compilers, but it does work with other compilers. I am not sure if that is true or not.

                  Funny thing is, if I use regular arrays, it doesn't work, but if I use a class like vectors, it does. This code for example, works perfectly, and both catch blocks are executed:


                  Code:
                      std::vector<int> v;
                  
                      try
                      {
                          v.at(10) = 1;
                      }
                      catch (std::out_of_range)
                      {
                          std::cout << "caught std::out_of_range\n";
                      }
                  
                      try
                      {
                          v.at(10) = 1;
                      }
                      catch (...)
                      {
                          std::cout << "EXCEPTION: " << e.what() << "\n";
                      }
                  The point is, if it is legal in other C++ compilers, then there has to be a similar command instead of (...)...but why does it work for classes and not arrays?

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    Originally posted by ahammad
                    I get an error and the console application crashes. Shouldn't the program catch the error?
                    Let's try this again.

                    Unless there is a throw, there is no exception. If there is no exception, then there is nothing to catch.

                    This example:
                    Originally posted by ahammad
                    int num[2];
                    num[0] = 5;
                    num[1] = 6;

                    int testvalue;


                    try
                    {
                    testvalue = num[9];
                    }
                    catch (...)
                    {
                    std::cout << "EXCEPTION caught \n";

                    }

                    int x;
                    cin >> x;
                    just corrupts memory. Look at the code. Do you see a throw instruction??
                    No. Hence, no exception. Hence, nothing to catch.

                    As to
                    [code=cpp]
                    testvalue = num[9];
                    [/code]

                    All this does is load an int (testvalue) with the contents of element 9 of the array num. The fact that num has only two elements doesn't matter. The compiler just takes the address of the array plus 9 times the sizeof(int) and plops the values at that memory address into testvaue. You are responsible for correct memory management, not the compiler.

                    Comment

                    • gnanapoongothai
                      New Member
                      • Jun 2007
                      • 62

                      #11
                      Sorry to to start a new query here, if try catch is used to handle exception then what about HANDLES in .could u ppl can explain it what is doing exactly

                      Comment

                      • weaknessforcats
                        Recognized Expert Expert
                        • Mar 2007
                        • 9214

                        #12
                        Please start a new discussion.

                        Comment

                        Working...