C++ (Qt)class ResourceManager{public: explicit ResourceManager( const std::string &dataDir ) throw( BadDataDir );};
C++ (Qt)std::shared_ptr<ResourceManager> Core::initResourceManager(){ std::vector< std:string > dataDirs = { "~/mysystem/data", "/usr/share/mysystem/data", "/opt/mysystem/data" }; for( auto dir : dataDirs ) { try { return std::make_shared<ResourceManager>( dir ); } catch( const BadDataDir & ) { log << "Directory " << dir.c_str() << " does not contain system data." } } // Вышли сюда, значит не в одной директории нет данных, сообщаем об этом выше, может кто-то и будет знать что с этим делать... throw DataNotFound;}
C++ (Qt)void doSomething1( int a, int b ) throw(std::out_of_range, std::invalid_argument) { // ... throw ( std::out_of_range( "...out of range..." ) ); // ... // ... throw ( std::invalid_argument( "...invalid argument..." ) ); // ...} void doSomething2( int a, int b ) throw(std::out_of_range, std::invalid_argument) { // ... throw ( std::out_of_range( "...out of range..." ) ); // ... // ... throw ( std::invalid_argument( "...invalid argument..." ) ); // ...}
C++ (Qt) int result = 0; try { result = sum( firstNumber, secondNumber ); } catch ( const LogicError &e ) { std::cerr << e.what( ) << std::endl; return 1; } catch ( ... ) { std::cerr << "Uncaught exception." << std::endl; return 1; }
C++ (Qt)int sum( int firstNumber, int secondNumber ) throw (OutOfRange) { int const beginOfRange = -1000000000; int const endOfRange = 1000000000; if ( (firstNumber < beginOfRange) || (endOfRange < firstNumber) ) { throw ( OutOfRange( firstNumber, beginOfRange, endOfRange )); } if ( (secondNumber < beginOfRange) || (endOfRange < secondNumber) ) { throw ( OutOfRange( secondNumber, beginOfRange, endOfRange )); } int result = firstNumber + secondNumber; return result;}
C++ (Qt)#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 ); }}
C++ (Qt)#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
C++ (Qt)#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
C++ (Qt)#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
C++ (Qt)#include <iostream>#include <vector>#include <stdexcept> int main(){ std::vector<int> arr = { 1, 2, 3 }; try { std::cout << arr.at( 3 ) << std::endl; } catch( std::logic_error &e ) { std::cerr << "Error: out of range" << std::endl; return 1; } catch( ... ) { std::cerr << "Error: " << std::endl; return 1; } return 0;}