Click here to Skip to main content
15,907,392 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have a simple question and would be nice if someone could answer.

How much is it a good idea for example when I am writing a class say
CSomeClass

that I make it's methods throw exceptions.

Than on the other hand, whoever may have to use this class, should put the
code where CSomeclass is used, in
try{} 
block.

e.g., place where CSomeclass is being used
C++
try
{
CSomeclass b;
b.f1();
...

}
catch (CSomeclass exception)
{

}


thanks.

or should I make the functions just return error values instead?
Posted
Updated 31-Oct-12 1:06am
v2

If you catch the exception, can you fix it in such a way that the function will return like normal without the calling code having to know about it, than yes. But if you cannot do anything useful to resolve the situation, what is the value of caching the exception?

Also, have a look here:
Exception Handling Best Practices in .NET[^]

Good luck!
 
Share this answer
 
Comments
Orjan Westin 31-Oct-12 8:51am    
Standard C++ have slightly different exception concerns than Managed C++/.NET does, in particular when it comes to resource management.
This can not be answered in one line as it is generally based on circumstances of how the class is being used and sometimes on the architecture's personal view-point.

I would personally think if it is in same layer you may throw it but in case if it going across layer boundary i.e. from data layer to business layer or from business layer to UI layer (in standard 3-tier architecture), then its better to handle it in the class and then return appropriate error message.

As I said, this is a debatable topic, and if you search on Google for "throwing vs catching exception" it will return you good amount of reading material.

Hope that helps or atleast provides some direction.

Thanks
Milind
 
Share this answer
 
Comments
[no name] 31-Oct-12 7:47am    
hi thanks. i think the problem with this approach is that this way we basically force the client to use try/catch whenever he needs to use CSomeclass. maybe error codes are better alternative. i will read more on it...........
MT_ 31-Oct-12 7:49am    
Exactly, when you are crossing boundary, you should use use error code/message strategy. This way you can keep door open for your class to be consumed by technology which may not understand .Net exception. :) Milind
There are no definitive answers, but here are some things to keep in mind:

* If an error is expected and can be easily handled, this is better done in a return code.

* If an error occurs in a constructor, it should throw, as there is no way of returning an error code. Old-style C++ coding practice was to have a two-stage construction, where you first construct and then call a separate Create function to be able to get an error code. This style is now discouraged, and exceptions are the recommended way of handling errors in construction.

* Your code is not the only one that can throw exceptions. If any functions or libraries your code calls can throw exceptions, the user of your class will have to try to catch exceptions at some level.

* You have been given some .NET references in comments and answers. If you are not using managed C++ (e.g. in .NET), you should consider that exceptions are exit points from functions, so that any resource that needs to be freed (dynamically allocated memory, file handles, semaphores etc) before an exit should be kept in a RAII-type container, like auto_ptr, shared_ptr etc.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900