473,322 Members | 1,671 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,322 software developers and data experts.

Why do i get uncaught exception and the program exits?

Expand|Select|Wrap|Line Numbers
  1. class Error {
  2. Error(string err) { cout << err; }
  3. }
  4. void main()
  5. {
  6.   try {
  7.         throw string("exception");
  8.  }
  9.  catch(Error Err)
  10. {
  11. }
  12. return
  13. }
  14.  
my question is :
why i get uncaught exception and the program exits ,
even though i have a Error constructor that gets string ?
thanks
Nov 30 '10 #1
15 2650
Oralloy
985 Expert 512MB
danielo,

You must excuse me, but I have to point out that your code is completely untested.

It won't compile due to syntax errors at lines 3 and 12.

In addition, the main function is supposed to return an int.

Please fix the syntax errors, as well as the problem with main, and re-post your question.

Good Luck!
Oralloy
Nov 30 '10 #2
Hi Danielo,

you have not coded to catch a string exception.
Or you have to code for generic catch.
Dec 1 '10 #3
i understand , but i am catching Error class which contains constructor that accepts string.. so i dont understand why not
Dec 1 '10 #4
Oralloy
985 Expert 512MB
danielo,

in C++, you catch arbitrary types.

why do you think that you are supposed to catch an arbitrary Err object that has no formal existance in the specification?

there is a standard exception class in the std namespace, which standard many C++ exceptions and errors are derived from. however, its use is not mandatory, like java.lang.Throwable is in Java. all the std::exception class does is provide a structured method of building exceptions.

so...throw a type, catch a type. in your case, you're throwing a string, but catching an Err, so your exception handler will not catch it.

if you were hoping for implicit type conversion, it's not done as far as I know. there is implicit up-casting, however there is no run-time search over the possible conversion space for an arbitrarily thrown object. your entire example is 13 lines, however C++ is required to allow separate compilation and linking of modules, and still work consistently.

BTW, did you ever fix your errors and run the thing?
Dec 1 '10 #5
weaknessforcats
9,208 Expert Mod 8TB
Also you do not catch objects. You catch references or pointers to objects. Otherwise, you create a new object in the catch block which isn't the one that was thrown:

Expand|Select|Wrap|Line Numbers
  1. catch (Error& Err)
  2. //etc...
Dec 1 '10 #6
Oralloy
985 Expert 512MB
@weaknessforcats,

Yep, that's the best way to go about it. However, I don't think that danielo understands the general concept of C++ error handling yet. I know that years ago, when try and catch were first introduced, that I used to throw strings (of the char* sort) on small projects, as they were quick and easy.

In fact, won't his example fail (assuming he throws an Err), because there is no appropriate copy constructor for his Err object? I don't have a C++ compiler handy, otherwise I'd test for my own benefit, thus my question.

Thanks,
Oralloy
Dec 1 '10 #7
hype261
207 100+
I don't believe it will fail because the compiler will create a copy constructor for him.

It won't print out the error string because that piece of logic is only defined in the constructor.
Dec 1 '10 #8
Oralloy
985 Expert 512MB
@hype261,

Sometimes the compiler can create a copy constructor, and other times it can't. I don't remember the rules. However, when there are embedded classes/objects involved, there are problems. I do know that the old C standard where structs are copied bitwise does not apply in this case.

Still, as I noted in my comment, I don't have a C++ compiler handy to test against. If you do, will you build and run the test, please?

Thanks,
Oralloy
Dec 1 '10 #9
weaknessforcats
9,208 Expert Mod 8TB
That's correct. C++ copies memberwise.

Therefore, the compiler copy constructor just calls the copy constructor of every member of your class.

You can supersede the compiler copy constructor by one of your own if you need more than is offered by the compiler version.
Dec 1 '10 #10
Oralloy
985 Expert 512MB
@weaknessforcats,

Which means that the compiler cannot generate an implicit copy constructor for a class, which contains a member that does not have an accessable copy constructor. Are there other restrictions?

In this case, everything's known, so the copy constructor is obviated.

However, the exception handling path knows nothing about object copies at run-time, so it can't implicitly instantiate copy constructors, even if it were appropriate.

Thus, my next question - does the C++ specification require that the copy constructors be be implicitly implemented for all non-reference-type exception handlers? (Yes, I do realize that invoking copy constructors for primitive types and pointers is moot. These, however are conceptually optimized away, anyway)
Dec 1 '10 #11
i have a big application , i just gave an example of the situation , so i could understand the theory behind it , and for copy constructor , the compiler defines a default copy constructor which will activate the default copy constructor of each member , so no need for me to define one , and as for the exception class in stl (std..) , almost no body uses it because it is not good , i just want to understand why it is ok to catch base class of an exception and why it is illegal to implicit cast ( string to Err) since for example :

void func1(Error err);

and u make this call :
func1(string("that works!!!"));

this works while for exceptions it doesnt
thanks
Dec 1 '10 #12
hype261
207 100+
Orally,

I built and ran the program which I believe was your question and it works just fine.

Expand|Select|Wrap|Line Numbers
  1. class Error 
  2. public:
  3.     Error(std::string err) { std::cout << err; } 
  4. }; 
  5.  
  6.  
  7.  
  8. int main()
  9. {
  10.     try
  11.     {
  12.         throw Error("Help");
  13.     }
  14.     catch(Error Err)
  15.     {
  16.  
  17.     }
  18.  
  19.     return 0;
  20. }
The output was Help and the compilier did generate the copy constructor for me.
Dec 1 '10 #13
any idea why it wont catch it if you throw a string instead ?
Dec 1 '10 #14
Oralloy
985 Expert 512MB
danielo,

It won't catch a string, because the examples we currently have do not have exception handlers that catch string objects.

Of course, we can add an exception handler for strings if we want. Extending the example that @hype261 built:
Expand|Select|Wrap|Line Numbers
  1. class Error  
  2. {  
  3. public: 
  4.     Error(std::string err):str(err) { std::cout << err; }  
  5.     string str;
  6. };  
  7.  
  8.  
  9.  
  10. int main() 
  11.     try 
  12.     { 
  13.         throw string("Help");
  14.         throw Error("Help"); 
  15.     } 
  16.     catch(Error Err) 
  17.     { 
  18.       cout << "Caught Error: " << Err.str << endl;
  19.     } 
  20.     catch(string const &str) 
  21.     { 
  22.       cout << "Caught string: " << str << endl;
  23.     } 
  24.  
  25.     return 0; 
You'll have to add the appropriate includes, but this should work for you.
Dec 2 '10 #15
hype261
207 100+
If I had to make a guess on why exceptions don't support implicit conversions is because it would be very difficult to do.

Implicit conversions for functions are realitively easy thing for a compiler to do. This function needs a Class A, but you gave it Class B, but A can be constructed from B. Now consider the following code example.

Expand|Select|Wrap|Line Numbers
  1.  
  2. class Error
  3. {
  4. public:
  5.      Error(string c){cout << c;};
  6. };
  7.  
  8. void c()
  9. {
  10.      throw string("HELP");
  11. }
  12.  
  13. void b()
  14. {
  15.      c();
  16. }
  17.  
  18. void a()
  19. {
  20.      b();
  21. }
  22.  
  23. int main()
  24. {
  25.      try
  26.      {
  27.           a();
  28.      }
  29.      catch(Error err)
  30.      {
  31.      }
  32. }
In this situation the compilier is going to have to go three functions deep to see you are going to throw a string and then do the implicit conversion. This example is really simple, consider what would happen if this was a real application. The compiler could be 50 functions deep across multiple source files.
Dec 2 '10 #16

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

Similar topics

1
by: serge calderara | last post by:
Dear all, How can I handle correctly cascading exception? I generally use to catch exception ib all my function. This to identify each type the correct place that the exception occurs. What...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
2
by: ETL | last post by:
Hi, I think this question has an easy answer but I can't seem to find anything online that lists best practices for my problem. I have a C# app that uses an exception handling tree similar to...
3
by: George Economos | last post by:
Is the following code correct (managed c++ console app): // native classes #pragma unmanaged class MyException {}; class MyThrow { MyThrow() {
4
by: James Radke | last post by:
Hello, I am looking for guidance on best practices to incorporate effective and complete error handling in an application written in VB.NET. If I have the following function in a class module...
16
by: Chuck Cobb | last post by:
I'm implementing a centralized exception handling routine using the Enterprise Library Exception Management Application Block. I trap all unhandled exceptions to one place using the following...
3
by: Larry Herbinaux | last post by:
I have built an asychronous TCP Server that uses the thread pool. I have two levels of exception handling in case the handling of the inner catch block throws an exception. The outer catch block...
5
by: Bry | last post by:
I've created a class that offers an enhanced way of handling fatal exceptions. The class allows the user to optionaly submit a http based anonymous error report to myself, and also records details...
3
by: osudude | last post by:
What happens in the following scenario? 1.Via a DLL interface, a managed thread kicks off a potentially long running FOR loop operation encapsulated by a native C++ dll. This FOR loop has C++...
1
by: George2 | last post by:
Hello everyone, Such code segment is used to check whether function call or exception- handling mechanism runs out of memory first (written by Bjarne), void perverted() { try{
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.