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(); }
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); }
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(); }
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*)¶ms); 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); }
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); } }
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; }
int_type chosen_failure_behaviour() { if (m_chosen_failure_behaviour == return_eof) { return traits_type::eof(); } else { throw exception("Threw mock exception"); } }
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(); }
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"; }
void Digraph<K, V>::finish_loop(vector<K>& finish) { reverse_edges(); search_set.clear(); if (connect.size_of() != vertices.size()) { throw exception(); } connect.reset(); for (int i = finish.size()-1; i >= 0; i--) { if (search_set.count(finish[i]) == 0) { vector< Vertex<K, V>*> tra; dfs_loop(vertices[finish[i]], back_inserter(tra)); analyse_part(tra); } } }
void render( STIPPLER_HANDLE stippler, const Voronoi::StipplingParameters ¶meters ) { 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; }
// 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
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!"); }
void slot_machine::get_bet(unsigned int money) { if (!money) throw exception("The player didn't make bet!"); bet = money; }
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(); } }
// 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 }
virtual pos_type __CLR_OR_THIS_CALL seekpos(pos_type, std::ios_base::openmode = std::ios_base::in | std::ios_base::out) { throw exception("Surprise!"); }
int func_throw_exception(void) { throw exception(); cout << "func_throw_exception: after throw" << endl; return 0; }