473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Exception handling in Visual Studio 2005

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

Expand|Select|Wrap|Line Numbers
  1. try{
  2.  
  3. [something]
  4.  
  5. } catch (...) {
  6.  
  7. [something]
  8.  
  9. }
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?
Jun 21 '07 #1
11 3251
weaknessforcats
9,208 Expert Mod 8TB
This code:
try{

//[something]

} catch (...) {

//[something]

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

Have you enabled exception handling in your project properties??
Jun 21 '07 #2
Silent1Mezzo
208 100+
When using the try catch block in Visual Studio 2005, we cannot have:

Expand|Select|Wrap|Line Numbers
  1. try{
  2.  
  3. [something]
  4.  
  5. } catch (...) {
  6.  
  7. [something]
  8.  
  9. }
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 ?

Expand|Select|Wrap|Line Numbers
  1. try{
  2.  
  3. [something]
  4.  
  5. } catch (Exception e) {
  6.  
  7. [something]
  8.  
  9. }
Jun 21 '07 #3
ahammad
79
Sure it compiles, but whatever is in the catch block doesn't get executed in my case.

Expand|Select|Wrap|Line Numbers
  1.     int nums[1];
  2.     nums[0] = 5;
  3.     try
  4.     {
  5.         int x = nums[99];
  6.         int y = 2;
  7.     }
  8.     catch (...)
  9.     {
  10.         TRACE("Catch\n");
  11.     }
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.
Jun 21 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
This code:

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.

Expand|Select|Wrap|Line Numbers
  1. try{
  2.  
  3. [something]
  4.  
  5. } catch (Exception e) {  <<<<Exception& or Exception* !!
  6.  
  7. [something]
  8.  
  9. }
  10.  
Jun 21 '07 #5
ahammad
79
when I do something like this:

Expand|Select|Wrap|Line Numbers
  1. int num[2];
  2. num[0] = 5;
  3. num[1] = 6;
  4.  
  5. int testvalue;
  6.  
  7.  
  8. try
  9. {
  10.     testvalue = num[9];
  11. }
  12. catch (...)
  13. {
  14.     std::cout << "EXCEPTION caught \n";
  15.  
  16. }
  17.  
  18. int x;
  19. cin >> x;
I get an error and the console application crashes. Shouldn't the program catch the error?
Jun 21 '07 #6
RedSon
5,000 Expert 4TB
when I do something like this:

Expand|Select|Wrap|Line Numbers
  1. int num[2];
  2. num[0] = 5;
  3. num[1] = 6;
  4.  
  5. int testvalue;
  6.  
  7.  
  8. try
  9. {
  10.     testvalue = num[9];
  11. }
  12. catch (...)
  13. {
  14.     std::cout << "EXCEPTION caught \n";
  15.  
  16. }
  17.  
  18. int x;
  19. cin >> x;
I get an error and the console application crashes. Shouldn't the program catch the error?
what is in your catch parens?
Jun 21 '07 #7
ahammad
79
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:


Expand|Select|Wrap|Line Numbers
  1.     std::vector<int> v;
  2.  
  3.     try
  4.     {
  5.         v.at(10) = 1;
  6.     }
  7.     catch (std::out_of_range)
  8.     {
  9.         std::cout << "caught std::out_of_range\n";
  10.     }
  11.  
  12.     try
  13.     {
  14.         v.at(10) = 1;
  15.     }
  16.     catch (...)
  17.     {
  18.         std::cout << "EXCEPTION: " << e.what() << "\n";
  19.     }
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?
Jun 21 '07 #8
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.


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:


Expand|Select|Wrap|Line Numbers
  1.     std::vector<int> v;
  2.  
  3.     try
  4.     {
  5.         v.at(10) = 1;
  6.     }
  7.     catch (std::out_of_range)
  8.     {
  9.         std::cout << "caught std::out_of_range\n";
  10.     }
  11.  
  12.     try
  13.     {
  14.         v.at(10) = 1;
  15.     }
  16.     catch (...)
  17.     {
  18.         std::cout << "EXCEPTION: " << e.what() << "\n";
  19.     }
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?
Jun 22 '07 #9
weaknessforcats
9,208 Expert Mod 8TB
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:
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
Expand|Select|Wrap|Line Numbers
  1.  testvalue = num[9];
  2.  
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.
Jun 22 '07 #10
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
Jun 28 '07 #11
weaknessforcats
9,208 Expert Mod 8TB
Please start a new discussion.
Jun 28 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: David B | last post by:
Why is it so difficult to report bugs to Microsoft? I have a documented bug and an small test example. I don't really see why I should have to pay to tell them about it... Anyway, the...
4
by: Ele | last post by:
When Exception handling disabled compiler still spits out "C++ exception handler used." Why is that? Why does it ask for "Specify /EHsc"? Thanks! c:\Program Files\Microsoft Visual Studio...
10
by: WhiskyRomeo | last post by:
I have webservices functions that return datasets. If a database errors happens, try catch statements can be used in the webservice to detect and handle them. However, how do I return that...
1
by: Reuben | last post by:
Hi, I've installed Visual C# Express 2005 onto 2 computers: one which already had Visual Studio .NET 2003 installed on it, and another that was a clean installation of Windows (both are Xp Pro)....
6
by: Robin Riley | last post by:
Hi, I have a .NET solution that contains a dll project and a tester application project which, of course, invokes the dll. The dll project has exception handling in it. What's happening is that...
2
by: Richard Collette | last post by:
Hi, I have a service, that runs perfectly when executed outside of the web service environment. When called as a web service I get the exception listed below sporadically. A call to the web...
4
by: CharlesA | last post by:
Hi folks, I'm working on code from Herb Schildt's book in which he says that an exception can be thrown by one method and caught by another class ExcTest { public static void genException() {...
24
by: Earl | last post by:
I have all of my data operations in a separate library, so I'm looking for what might be termed "best practices" on a return type from those classes. For example, let's say I send an update from...
0
by: srizzler | last post by:
Hi All: I am trying to implement Exception Handling using Enterprise Library 3.1's Exception Handling Application Block as well as Logging Blocks. I have a windows application developed in...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.