Exception handling class syntax

Oct 14, 2014 at 5:29pm
Hello. I am confused with the code [commented]:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
include <iostream>
using namespace std;

class dividebyzero{
	public:	
	 dividebyzero(); 		
         void printmessage();	
        
        private:		
         const char* message;
};
//I dont understand this function syntax. Why : message() is added?
dividebyzero::dividebyzero() : message(“Divide by Zero”)

{	/* nothing else needs to be done */}void 

dividebyzero::printmessage(){
	cout << message << endl;}	
Oct 14, 2014 at 5:34pm
That's an initialisation list.
Oct 14, 2014 at 5:39pm
What do you mean? How initialisation list?
Oct 14, 2014 at 6:05pm
That's just how constructors are written in C++. Here's a summary http://en.cppreference.com/w/cpp/language/initializer_list
Oct 14, 2014 at 6:24pm
so there are more than 1 way to write copy constructor? I mean whats the difference between that and this

// Copy constructor of class Point
Point:: Point(const Point &p2)
{x = p2.x; y = p2.y; }
Last edited on Oct 14, 2014 at 6:43pm
Oct 14, 2014 at 6:47pm
1
2
3
4
5
6
SomeClass::SomeClass(type1 param1, type2 param2, type3 param3)
: member1(param1)
, member2(param2)
, member3(param3)
{
}
The above code constructs each of member1, member2, member3 from each of param1, param2, param3.
1
2
3
4
5
6
SomeClass::SomeClass(type1 param1, type2 param2, type3 param3)
{
    member1 = param1;
    member2 = param2;
    member3 = param3;
}
The above code constructs each of member1, member2, member3 via their default constructors, and then after that calls their assignment operators with each of param1, param2, param3. It is less efficient by far.
Last edited on Oct 14, 2014 at 6:48pm
Oct 14, 2014 at 7:46pm
Exception handling class syntax


I use a strategy from this book: http://www.amazon.com/Professional-C-Marc-Gregoire/dp/0470932449

Output
Error: divide by zero in the function func()


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include "AnotherError.h"
#include "DivideByZero.h"

void func( int a, int b )
throw( DivideByZero, AnotherError );

int main()
{
    try {
        func( 28, 0 );
    } catch ( const LogicError &e ) {
        std::cerr << e.what() << std::endl;
        return 1;
    } catch ( ... ) {
        std::cerr << "Error: unknown expection" << std::endl;
        return 1;
    }

    return 0;
}

void func( int a, int b )
throw( DivideByZero, AnotherError )
{
    std::string functionName = "func()";

    if ( b == 0 ) {
        throw DivideByZero( functionName );
    }

    // ...

    if ( a == 5 ) {
        throw AnotherError( functionName );
    }
}


DivideByZero.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef DIVIDEBYZERO_H
#define DIVIDEBYZERO_H

#include <string>
#include "LogicError.h"

class DivideByZero : public LogicError
{
public:
    DivideByZero( const std::string &functionName ) :
        LogicError( functionName )
    {
        m_message = "Error: divide by zero in the "
                "function " + m_functionName;
    }
};

#endif // DIVIDEBYZERO_H 


AnotherError.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef ANOTHERERROR_H
#define ANOTHERERROR_H

#include <string>
#include "LogicError.h"

class AnotherError : public LogicError
{
public:
    AnotherError( const std::string &functionName ) :
        LogicError( functionName )
    {
        m_message = "Error: some error in the "
                "function " + m_functionName;
    }
};

#endif // ANOTHERERROR_H 


LogicError.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#ifndef LOGICERROR_H
#define LOGICERROR_H

#include <string>
#include <stdexcept>

class LogicError : public std::logic_error
{
public:

    LogicError( const std::string &functionName ) :
        std::logic_error( "" ),
        m_functionName( functionName ),
        m_message( "" )
    {

    }

    virtual ~LogicError( ) throw( )
    {

    }

    virtual const char *what( ) const throw( )
    {
        return m_message.c_str( );
    }

    std::string message( ) const
    {
        return m_message;
    }

protected:
    std::string m_functionName;
    std::string m_message;
};

#endif // LOGICERROR_H 
Oct 14, 2014 at 10:17pm
I think the title of the topic was a mistake.
Topic archived. No new replies allowed.