Results 1 to 5 of 5

Thread: Can I throw exceptions from the constructor?

  1. #1
    Join Date
    Nov 2012
    Posts
    232
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Platforms
    Windows Android

    Default Re: Can I throw exceptions from the constructor?

    Hello

    Sorry for my English in advance!

    Can I throw exceptions from the constructor?

    I want to send data to the Serial Port. And I want to control errors via exceptions. I've written my own exception for it:

    PortError.h
    Qt Code:
    1. #ifndef PORTERROR_H
    2. #define PORTERROR_H
    3.  
    4. #include <stdexcept>
    5. #include <string>
    6.  
    7. class PortError : public std::runtime_error
    8. {
    9. public:
    10. PortError( const std::string &errorText ) : std::runtime_error( "" )
    11. {
    12. m_message = "Error: " + errorText;
    13. }
    14.  
    15. virtual ~PortError() throw()
    16. {
    17.  
    18. }
    19.  
    20. virtual const char *what() const throw()
    21. {
    22. return m_message.c_str();
    23. }
    24.  
    25. std::string getMessage()
    26. {
    27. return m_message;
    28. }
    29.  
    30. private:
    31. std::string m_message;
    32. };
    33.  
    34. #endif // PORTERROR_H
    To copy to clipboard, switch view to plain text mode 

    I will use it like this:
    Qt Code:
    1. void MainWindow::on_startTransmissionButton_clicked()
    2. {
    3. QString text = ui->valueForSendingLineEdit->text();
    4. QByteArray data;
    5. data.append( text );
    6. try {
    7. m_sender->send( data );
    8. } catch ( const PortError &e ) {
    9. QMessageBox::information( this, "Error", QString( e.what() ) );
    10. return;
    11. } catch ( ... ) {
    12. QMessageBox::information( this, "Error", "Error: unknown exception" );
    13. return;
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 

    Sender.h
    Qt Code:
    1. #ifndef SENDER_H
    2. #define SENDER_H
    3.  
    4. #include <QSerialPort>
    5. #include <QString>
    6. #include "PortError.h"
    7.  
    8. class Sender
    9. {
    10. public:
    11. Sender( const QString &portName,
    12. QSerialPort::BaudRate baudRate = QSerialPort::Baud9600,
    13. QSerialPort::DataBits dataBits = QSerialPort::Data8,
    14. QSerialPort::Parity parity = QSerialPort::NoParity,
    15. QSerialPort::StopBits stopBits = QSerialPort::OneStop,
    16. QSerialPort::FlowControl flowControl = QSerialPort::NoFlowControl );
    17.  
    18. ~Sender();
    19.  
    20. void send( const QByteArray &data ) throw( PortError );
    21.  
    22. private:
    23. QSerialPort m_port;
    24. QString m_portName;
    25. QSerialPort::BaudRate m_baudRate;
    26. QSerialPort::DataBits m_dataBits;
    27. QSerialPort::Parity m_parity;
    28. QSerialPort::StopBits m_stopBits;
    29. QSerialPort::FlowControl m_flowControl;
    30. };
    31.  
    32. #endif // SENDER_H
    To copy to clipboard, switch view to plain text mode 

    Sender.cpp
    Qt Code:
    1. #include "Sender.h"
    2.  
    3. Sender::Sender(const QString &portName,
    4. QSerialPort::BaudRate baudRate,
    5. QSerialPort::DataBits dataBits,
    6. QSerialPort::Parity parity,
    7. QSerialPort::StopBits stopBits,
    8. QSerialPort::FlowControl flowControl ) :
    9. m_port( portName ),
    10. m_baudRate( baudRate ),
    11. m_dataBits( dataBits ),
    12. m_parity( parity ),
    13. m_stopBits( stopBits ),
    14. m_flowControl( flowControl )
    15. {
    16. // Set the port name
    17. m_port.setPortName( m_portName );
    18.  
    19. // Open the port
    20. if ( !m_port.open( QIODevice::WriteOnly ) ) {
    21. throw PortError( m_port.errorString().toStdString() );
    22. }
    23.  
    24. m_port.setBaudRate( m_baudRate );
    25. m_port.setDataBits( m_dataBits );
    26. m_port.setParity( m_parity );
    27. m_port.setStopBits( m_stopBits );
    28. m_port.setFlowControl( m_flowControl );
    29. }
    30.  
    31. Sender::~Sender()
    32. {
    33. m_port.close();
    34. }
    35.  
    36. void Sender::send( const QByteArray &data ) throw( PortError )
    37. {
    38. // Write data to the port
    39. if ( m_port.write( data ) == -1 ) {
    40. throw PortError( m_port.errorString().toStdString() );
    41. }
    42. }
    To copy to clipboard, switch view to plain text mode 


    Added after 14 minutes:


    Can I use it? Is it right?

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. try {
    8. m_sender = new Sender( "COM1" );
    9. } catch ( const PortError &e ) {
    10. QMessageBox::information( this, "Error", QString( e.what() ) );
    11. } catch ( ... ) {
    12. QMessageBox::information( this, "Error", "Error: unknown exception" );
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by 8Observer8; 23rd August 2014 at 08:19.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Can I throw exceptions from the constructor?

    Yes, if you wish. Exceptions are a standard part of C++ and often the best way to handle unrecoverable problems in a constructor (you cannot use a return value). The destructor will not be called if the constructor throws, so you need to be careful to ensure anything the constructor has allocated before the exception is wrapped in a smart pointer or similar. (Does not seem to affect your example)

  3. The following user says thank you to ChrisW67 for this useful post:

    8Observer8 (23rd August 2014)

  4. #3
    Join Date
    Nov 2012
    Posts
    232
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Platforms
    Windows Android

    Default Re: Can I throw exceptions from the constructor?

    Thank you!

    My application crash when I call .isOpen() Method. Why?

    Qt Code:
    1. void Sender::send( const QByteArray &data ) throw( PortError )
    2. {
    3. if ( !m_port.isOpen() ) {
    4. throw PortError( m_port.errorString().toStdString() );
    5. }
    6.  
    7. // Write data to the port
    8. if ( m_port.write( data ) == -1 ) {
    9. throw PortError( m_port.errorString().toStdString() );
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Can I throw exceptions from the constructor?

    We can only guess. The actual error message/crash dump and your debugger will help you work it out. Post the stack backtrace and other details if you cannot work it out.

  6. #5
    Join Date
    Nov 2012
    Posts
    232
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Platforms
    Windows Android

    Default Re: Can I throw exceptions from the constructor?

    I was helped here: http://www.prog.org.ru/index.php?topic=27520

    The error on this line "m_port( portName ),"
    Qt Code:
    1. Sender::Sender( const QString &portName,
    2. QSerialPort::BaudRate baudRate,
    3. QSerialPort::DataBits dataBits,
    4. QSerialPort::Parity parity,
    5. QSerialPort::StopBits stopBits,
    6. QSerialPort::FlowControl flowControl ) :
    7. m_port( portName ),
    8. m_baudRate( baudRate ),
    9. m_dataBits( dataBits ),
    10. m_parity( parity ),
    11. m_stopBits( stopBits ),
    12. m_flowControl( flowControl )
    13. {
    14. // Set the port name
    15. m_port.setPortName( m_portName );
    To copy to clipboard, switch view to plain text mode 

    I should have written m_portName instead m_port.

    I replaced "QSerialPort m_port;" on "QSerialPort m_serialPort;" And I delete the m_portName because I can to write m_serialPort( portName ) in the "Sender" constructor.

    It is my project: https://github.com/8Observer8/ComPort
    Last edited by 8Observer8; 27th August 2014 at 09:46.

Similar Threads

  1. gcc exceptions
    By ctarsoaga in forum General Programming
    Replies: 1
    Last Post: 4th May 2010, 07:37
  2. Using exceptions with Qt
    By vu64 in forum Qt Programming
    Replies: 1
    Last Post: 17th May 2009, 03:28
  3. sql Exceptions
    By peace_comp in forum Qt Programming
    Replies: 2
    Last Post: 10th October 2008, 02:51
  4. QSA exceptions
    By seneca in forum Qt Programming
    Replies: 2
    Last Post: 2nd February 2006, 15:27
  5. QT and exceptions
    By krivenok in forum Qt Programming
    Replies: 4
    Last Post: 25th January 2006, 14:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.