Design question

I'm wondering which way is better for throwing exceptions?
This?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class C
{
  bool private_function(){/* ... */}
public:
  void public_function()
    {
        if(!private_function())
          {
              throw something;
          }
    }
    
    
};

or this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class D
{
  bool private_function()
    {
       if(error)
           throw something; 
    }
public:
  void public_function()
    {
       private_function()
    }
    
    
};

In general, which function should throw exceptions? public_function() or private_function() ?

Thanks for help.
There is no real defined way, but there is a certain commonly excepted guideline that states:

Use public for everything a 3rd party user (anyone who just dropped in) of your class would need to know. (This includes things like constness, staticness, mutability and exceptions).
Use private / protected for all the compatibilities within the class and it's family (like the handling of data members).

Since exceptions can alter the way a program flows, I recommend to put it in the public section. Just one side note, only define t he functions in the class it's self when you want them to be inlined functions.
Last edited on
So in theory the user only calls public_function(), and the implementation of some of your other methods
may call private_function(). Do you want those implementations to have try-catch blocks in them? Or
do you want the exceptions to leak and be caught by the user code? The answers to these questions I
think determine the answer to your original question. There probably isn't a generalized right answer.

Topic archived. No new replies allowed.