C++ (Qt)// out_of_range example#include <iostream> // std::cerr#include <stdexcept> // std::out_of_range#include <vector> // std::vector int main( void ) { std::vector<int> myvector( 10 ); try { myvector.at( 20 ) = 100; // vector::at throws an out-of-range } catch ( const std::out_of_range& oor ) { std::cerr << "Out of Range error: " << oor.what( ) << '\n'; } return 0;}
C++ (Qt) // Read data from a file QString iFileName = QString( "input.txt" ); QVector<int> arr; try { readData( iFileName, arr ); } catch ( const std::exception &e ) { std::cerr << e.what( ) << std::endl; return 1; }
C++ (Qt) // Read data from a file QString iFileName = QString( "input.txt" ); errorCode = readData( iFileName, data ); if ( errorCode != ErrorType::errNone ) { return showError( iFileName, errorCode ); }
C++ (Qt)strm >> a;if (strm.error()) ... // throw или возврат ошибки...strm >> b;if (strm.error()) ... ...strm >> c;if (strm.error()) ... ....
C++ (Qt)void readData( const QString &fileName, QVector<int> &dest )throw (std::invalid_argument, std::runtime_error) { // ... std::string error = "Unable to open the file " + fileName.toStdString( ); throw std::invalid_argument( error ); // ... // ... std::string error = "Unable to read file " + fileName.toStdString( ); throw std::runtime_error( error ); // ...}
C++ (Qt)void readData( const QString &fileName, QVector<int> &dest )throw (FileOpenError, FileReadError) { // ... throw FileOpenError( fileName ); // ... // ... throw FileReadError( fileName, lineNumber ); // ... }
C++ (Qt)#ifndef FILEOPENERROR_H#define FILEOPENERROR_H #include <QString>#include "FileError.h" class FileOpenError : public FileError {public: FileOpenError( const QString &fileNameIn ) : FileError( fileNameIn ) { mMsg = "Unable to open " + fileNameIn.toStdString( ); }}; #endif // FILEOPENERROR_H
C++ (Qt)#ifndef FILEREADERROR_H#define FILEREADERROR_H #include "FileError.h"#include <QString>#include <QTextStream>#include <sstream> class FileReadError : public FileError {public: FileReadError( const QString &fileNameIn, int lineNumIn ) : FileError( fileNameIn ), mLineNum( lineNumIn ) { std::ostringstream ostr; ostr << "Error reading " << fileNameIn.toStdString( ) << " at line " << lineNumIn; mMsg = ostr.str( ); } int getLineNum( ) const { return mLineNum; } protected: int mLineNum;}; #endif // FILEREADERROR_H
C++ (Qt)#ifndef FILEERROR_H#define FILEERROR_H #include <QString>#include <exception>#include <string> class FileError : public std::runtime_error {public: FileError( const QString &fileIn ) : std::runtime_error( "" ), mFile( fileIn ) { } virtual const char* what( ) const noexcept { return mMsg.c_str( ); } QString getFileName( ) const { return mFile; } protected: QString mFile; std::string mMsg;}; #endif // FILEERROR_H
C++ (Qt)int main( int argc, char *argv[] ) { // ... // Read data from a file QString iFileName = QString( "input.txt" ); QVector<int> arr; try { readData( iFileName, arr ); } catch ( const FileError &e ) { std::cerr << e.what( ) << std::endl; return 1; } // ... }
C++ (Qt) string outFileName = "output.txt"; ofstream out; out.open( outFileName.c_str( ) ); if ( !out.is_open( ) ) { cerr << "Error: could not open the file " << outFileName.c_str( ) << endl; in.close( ); return 1; }