當前位置: 首頁>>代碼示例>>C++>>正文


C++ std::exception方法代碼示例

本文整理匯總了C++中std::exception方法的典型用法代碼示例。如果您正苦於以下問題:C++ std::exception方法的具體用法?C++ std::exception怎麽用?C++ std::exception使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在std的用法示例。


在下文中一共展示了std::exception方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C++代碼示例。

示例1: add_traits

void trait::add_traits() {
	ifstream file("trait.csv");
	if (!file.is_open())
		throw exception("File \"traits.csv\" is not found.");

	int current_index = 0, test_index;

	string buffer;
	while (!file.eof()) {
		getline(file, buffer);

		vector<string> values = parse_csv_line(buffer);
		auto it = values.begin();

		stringstream ss(*it);
		ss >> test_index;
		if (current_index != test_index) {
			file.close();
			throw exception("There's a mistake in \"traits.csv\".");
		}

		traits.push_back(trait(*(++it)));
		++current_index;
	}

	file.close();
}
開發者ID:3A9C,項目名稱:ITstep,代碼行數:27,代碼來源:trait.cpp

示例2: exception

Transmitter::Transmitter(string filename, double frequency) :
    readStdin(filename == "-")
{
    ostringstream oss;
    bool isBcm2835 = true;

    FILE *pipe = popen("uname -m", "r");
    if (pipe) {
        char buffer[64];
        string machine = "";
        while (!feof(pipe)) {
            if (fgets(buffer, 64, pipe)) {
                machine += buffer;
            }
        }
        pclose(pipe);

        if (machine != "armv6l\n") {
            isBcm2835 = false;
        }
    }

    int memFd;
    if ((memFd = open("/dev/mem", O_RDWR | O_SYNC)) < 0) {
        oss << "Cannot open /dev/mem (permission denied)";
        errorMessage = oss.str();
        throw exception();
    }

    void *peripheralsMap = mmap(NULL, 0x002FFFFF, PROT_READ | PROT_WRITE, MAP_SHARED, memFd, isBcm2835 ? 0x20000000 : 0x3F000000);
    close(memFd);
    if (peripheralsMap == MAP_FAILED) {
        oss << "Cannot obtain access to peripherals (mmap error)";
        errorMessage = oss.str();
        throw exception();
    }

    peripherals = (volatile unsigned*)peripheralsMap;

    AudioFormat *audioFormat;
    if (!readStdin) {
        waveReader = new WaveReader(filename);
        audioFormat = waveReader->getFormat();
    } else {
        stdinReader = StdinReader::getInstance();
        audioFormat = stdinReader->getFormat();
        usleep(700000);
    }
    format = *audioFormat;

    clockDivisor = (unsigned int)((500 << 12) / frequency + 0.5);
}
開發者ID:codeskyblue,項目名稱:fm_transmitter,代碼行數:52,代碼來源:transmitter.cpp

示例3: add_answers

void add_answers() {
	ifstream file("answers.csv");
	if (!file.is_open())
		throw exception("File \"answers.csv\" is not found.");

	string buffer, answer_text;
	size_t question_index;
	trait_num tr_num;
	int tr_sc;

	while (!file.eof()) {
		getline(file, buffer);

		vector<string> values = parse_csv_line(buffer);

		auto it = values.begin();
		stringstream ss(*(it++));
		ss >> question_index;
		answer_text = *(it++);

		vector<answer::point> points;
		while (it != values.end()) {
			stringstream trait_number(*(it++)), trait_score(*(it++));
			trait_number >> tr_num;
			trait_score >> tr_sc;
			points.push_back(answer::point(tr_num, tr_sc));
		}
		test::questions[question_index].push(answer(answer_text, points));
	}

	file.close();
}
開發者ID:3A9C,項目名稱:ITstep,代碼行數:32,代碼來源:test.cpp

示例4: play

void Transmitter::play()
{
    ostringstream oss;

    if (isTransmitting) {
        oss << "Cannot play, transmitter already in use";
        errorMessage = oss.str();
        throw exception();
    }

    frameOffset = 0;
    isTransmitting = true;
    pthread_t thread;

    unsigned int bufferFrames = (unsigned int)((unsigned long long)format.sampleRate * BUFFER_TIME / 1000000);

    buffer = (!readStdin) ? waveReader->getFrames(bufferFrames, frameOffset) : stdinReader->getFrames(bufferFrames);

    vector<void*> params;
    params.push_back((void*)&format.sampleRate);
    params.push_back((void*)&clockDivisor);
    params.push_back((void*)&frameOffset);
    params.push_back((void*)&buffer);
    params.push_back((void*)peripherals);

    int returnCode = pthread_create(&thread, NULL, &Transmitter::transmit, (void*)&params);
    if (returnCode) {
        oss << "Cannot create new thread (code: " << returnCode << ")";
        errorMessage = oss.str();
        throw exception();
    }

    usleep(BUFFER_TIME / 2);

    while(readStdin || !waveReader->isEnd()) {
        if (buffer == NULL) {
            buffer = (!readStdin) ? waveReader->getFrames(bufferFrames, frameOffset + bufferFrames) : stdinReader->getFrames(bufferFrames);
        }
        usleep(BUFFER_TIME / 2);
    }

    isTransmitting = false;
    pthread_join(thread, NULL);
}
開發者ID:codeskyblue,項目名稱:fm_transmitter,代碼行數:44,代碼來源:transmitter.cpp

示例5: exception

void Digraph<K, V>::set_edge_istream(Reader& r) {
    int t, h;
    while (r.next_int(t)) {
        if (!r.next_int(h)) {
            throw exception();
        }
        // reverse the tail and head at first
        this->add_edge(h-GAP, t-GAP);
    }
}
開發者ID:570468837,項目名稱:Daily-pracitce,代碼行數:10,代碼來源:scc.cpp

示例6: return_win

unsigned int slot_machine::return_win() {
	if (win > storage)
		throw exception("Automat doesn't have enough money. Call the administrator.");
	else
		storage -= win;

	unsigned int return_win = win;
	win = 0;

	return return_win;
}
開發者ID:a-pinch,項目名稱:andrewtroyan,代碼行數:11,代碼來源:slot_machine.cpp

示例7: chosen_failure_behaviour

 int_type chosen_failure_behaviour()
 {
     if (m_chosen_failure_behaviour == return_eof)
     {
         return traits_type::eof();
     }
     else
     {
         throw exception("Threw mock exception");
     }
 }
開發者ID:alamaison,項目名稱:comet,代碼行數:11,代碼來源:stream.cpp

示例8: init

void profession::init() {
	ifstream file("professions.csv");
	if (!file.is_open())
		throw exception("File \"professions.csv\" is not found.");

	int current_index = 0, test_index;
	string buffer;

	while (!file.eof()) {
		getline(file, buffer);

		vector<string> values = parse_csv_line(buffer);
		auto it = values.begin();

		stringstream ss(*(it++));
		ss >> test_index;
		if (current_index != test_index) {
			file.close();
			throw exception("There's a mistake in \"professions.csv\".");
		}

		string profession_name = *(it++);
		vector<trait_num> related_traits;
		trait_num tr_number;

		while (it != values.end()) {
			stringstream ss_trait(*(it++));
			ss_trait >> tr_number;
			related_traits.push_back(tr_number);
		}
		
		const_cast<vector<profession>&>(profession::professions).push_back(profession(profession_name, related_traits));
		++current_index;
	}

	file.close();
}
開發者ID:a-pinch,項目名稱:andrewtroyan,代碼行數:37,代碼來源:professions.cpp

示例9: throwException

void throwException()
{
    // Throw an exception and immediately catch it.
    try {
        cout << "Function throwException\n";
        throw exception();  // generate exception
    }
    catch( exception e )
    {
        cout << "Exception handled in function throwException\n";
        throw;  // rethrow exception for further processing
    }

    cout << "This also should not print\n";
}
開發者ID:tjyang,項目名稱:abmon,代碼行數:15,代碼來源:fig13_02.cpp

示例10: render

void render( STIPPLER_HANDLE stippler, const Voronoi::StipplingParameters &parameters ) {
	StipplePoint *points = new StipplePoint[parameters.points];

	stippler_getStipples(stippler, points);

	using namespace std;

	ofstream outputStream( parameters.outputFile.c_str() );

	if ( !outputStream.is_open() ) {
		stringstream s;
		s << "Unable to open output file " << parameters.outputFile;
                std::cout << s.str().c_str() << std::endl;
		throw exception();
	}

	PNG::PNGFile *png = PNG::load( parameters.inputFile );
	unsigned long w = png->w, h = png->h;
	PNG::freePng(png);

	outputStream << "<?xml version=\"1.0\" ?>" << endl;
	outputStream << "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">" << endl;
	outputStream << "<svg width=\"" << w << "\" height=\"" << h << "\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">" << endl;
	
	float radius;
	for ( unsigned int i = 0; i < parameters.points; ++i ) {
		if ( parameters.fixedRadius ) {
			radius = 0.5f; // gives circles with 1px diameter
		} else {
			radius = points[i].radius;
		}
		radius *= parameters.sizingFactor;

		if ( !parameters.useColour ) {
			points[i].r = points[i].g = points[i].b = 0;
		}

		outputStream << "<circle cx=\"" << points[i].x << "\" cy=\"" << points[i].y << "\" r=\"" << radius << "\" fill=\"rgb(" << (unsigned int)points[i].r << "," << (unsigned int)points[i].g << "," << (unsigned int)points[i].b << ")\" />" << endl;
	}
	outputStream << "</svg>" << endl;

	outputStream.close();

	delete[] points;
}
開發者ID:applio,項目名稱:voronoi,代碼行數:45,代碼來源:voronoi.cpp

示例11: throwException

// throw, catch and rethrow exception
void throwException() 
{
   // throw exception and catch it immediately
   try {
      cout << "  Function throwException throws an exception\n";
      throw exception();  // generate exception

   } // end try

   // handle exception
   catch ( exception &caughtException ) {
      cout << "  Exception handled in function throwException"
           << "\n  Function throwException rethrows exception";

      throw;  // rethrow exception for further processing

   } // end catch

   cout << "This also should not print\n";

}  // end function throwException
開發者ID:miaowangtw,項目名稱:Old-Codes,代碼行數:22,代碼來源:fig13_02.cpp

示例12: seekoff

 virtual pos_type seekoff(off_type, std::ios_base::seekdir,
                          std::ios_base::openmode = std::ios_base::in |
                                                    std::ios_base::out)
 {
     throw exception("Surprise!");
 }
開發者ID:alamaison,項目名稱:comet,代碼行數:6,代碼來源:stream.cpp

示例13: get_bet

void slot_machine::get_bet(unsigned int money) {
	if (!money)
		throw exception("The player didn't make bet!");
	bet = money;
}
開發者ID:a-pinch,項目名稱:andrewtroyan,代碼行數:5,代碼來源:slot_machine.cpp

示例14: parseArguments

std::auto_ptr<Voronoi::StipplingParameters> parseArguments( int argc, char *argv[] ) {
	using namespace boost::program_options;

	using std::auto_ptr;
	using std::string;
	using std::cout;
	using std::cerr;
	using std::endl;
	using std::runtime_error;
	using std::exception;

	options_description requiredOpts( "Required Options" );
	requiredOpts.add_options()
		( "input-file,I", value< string >()->required(), "Name of input file" )
		( "output-file,O", value< string >()->required(), "Name of output file" );

	options_description helpOpts( "Help Options" );
	helpOpts.add_options()( "help", "Help!" );

	options_description basicOpts( "Basic Options" );
	basicOpts.add_options()
		( "stipples,s", value< int >()->default_value(4000), "Number of Stipple Points to use" )
		( "colour-output,c", "Produce a coloured stipple drawing" );

	options_description advancedOpts( "Advanced Options" );
	advancedOpts.add_options()
		( "threshold,t", value< float >()->default_value(0.1f, "0.1"), "How long to wait for Voronoi diagram to converge" )
		( "no-overlap,n", "Ensure that stipple points do not overlap with each other" )
		( "fixed-radius,f", "Fixed radius stipple points imply a significant loss of tonal properties" )
		( "sizing-factor,z", value< float >()->default_value(1.0f, "1.0"), "The final stipple radius is multiplied by this factor" )
		( "subpixels,p", value< int >()->default_value(5, "5"), "Controls the tile size of centroid computations." )
		( "log,l", "Determines output verbosity" );

	positional_options_description positional;
	positional.add( "input-file", 1 );
	positional.add( "output-file", 1 );

	options_description all( "All Options" );
	all
		.add( requiredOpts )
		.add( basicOpts )
		.add( advancedOpts );

	// parse the parameters
	try {
		variables_map help_variables;
		store( command_line_parser( argc, argv ).options( helpOpts ).allow_unregistered().run(), help_variables );
		notify( help_variables );

		if ( help_variables.count( "help" ) > 0 ) {
			showSamples();
			cout << requiredOpts << endl;
			cout << basicOpts << endl;
			cout << advancedOpts << endl;

			return auto_ptr<Voronoi::StipplingParameters>( NULL );
		}

		variables_map vm;
		store( command_line_parser( argc, argv ).options( all ).positional( positional ).run(), vm );
		notify( vm );

		auto_ptr<Voronoi::StipplingParameters> params( new Voronoi::StipplingParameters() );

		string inputFile = vm["input-file"].as<string>();
		params->inputFile = new char[inputFile.length() + 1]; memset(params->inputFile, 0, inputFile.length() + 1);
		inputFile.copy(params->inputFile, inputFile.length());
		params->outputFile = vm["output-file"].as<string>();

		if (vm["stipples"].as<int>() <= 0) {
			throw runtime_error("Stipple renderings must have at least 1 stipple point.");
		}
		params->points = (unsigned int)vm["stipples"].as<int>();
		if (vm["threshold"].as<float>() < 0.005f) {
			throw runtime_error("Convergence threshold parameter must be greater than 0.005");
		}
		params->threshold = vm["threshold"].as<float>();
		params->createLogs = vm.count("log") > 0;
		params->useColour = vm.count("colour-output") > 0;
		params->noOverlap = vm.count("no-overlap") > 0;
		params->fixedRadius = vm.count("fixed-radius") > 0;
		if (vm["sizing-factor"].as<float>() < 0.0f) {
			throw runtime_error("Sizing factor parameter must be greater than 0.");
		}
		params->sizingFactor = vm["sizing-factor"].as<float>();
		if (vm["subpixels"].as<int>() <= 1) {
			throw runtime_error("Sub-pixel density parameter must be greater than or equal to 1.");
		}
		params->subpixels = (unsigned int)vm["subpixels"].as<int>();

		return params;
	} catch ( exception const &e ) {
		cerr << e.what() << endl;
		cout << endl;
		showSamples();
		cout << endl;
		cout << "For more information invoke help via \"voronoi --help\"." << endl;

		throw exception();
	}
//.........這裏部分代碼省略.........
開發者ID:kirannasim,項目名稱:MidTerm,代碼行數:101,代碼來源:parse_arguments.cpp

示例15: f

// this function will compile, even though it clearly violates its exception specification
void f() noexcept       // promises not to throw any exception
{
    throw exception();  // violates the exception specification
}
開發者ID:MGZX,項目名稱:cpp_primer,代碼行數:5,代碼來源:spec.cpp


注:本文中的std::exception方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。