// Only int and exception or derived excpetions are catchable: void exception_func_int_exception_only(int which) throw (int, std::exception) { switch (which) { case 0: throw 0; break; case 1: throw myexception(); break; default: throw 'c'; break; } }
void NonDefaultErrorHandler(Int_t level, Bool_t abort_bool, const char *location, const char *msg) { DefaultErrorHandler(level, kFALSE, location, msg); if (abort_bool) { throw myexception(msg); } }
void NEXUS::initialize() { // Check #NEXUS getline(*file,line); if (line != "#NEXUS") throw myexception()<<"NEXUS trees reader: File does not begin with '#NEXUS' and may not be a NEXUS file."; // [ and ] do not break words string word; bool in_trees_block=false; while(get_NEXUS_command(*file,line)) { // cerr<<"line: "<<line<<endl; int pos=0; if (not get_word_NEXUS(word,pos,line)) continue; // cerr<<"NEXUS: command = :"<<word<<":"<<" in_trees_block = "<<in_trees_block<<endl; // Parse BEGIN TREES if (uppercase(word) == "BEGIN") { if (not get_word_NEXUS(word,pos,line)) continue; if (uppercase(word) == "TREES") in_trees_block = true; } if (not in_trees_block) continue; // Parse TRANSLATE ... if (uppercase(word) == "TRANSLATE") { parse_translate_command(line.substr(pos,line.size()-pos)); // cerr<<"leaf names = "<<join(leaf_names,',')<<endl; line.clear(); return; } else if (uppercase(word) == "TREE") { try { get_word_NEXUS(word,pos,line); if (not (word == "=")) get_word_NEXUS(word,pos,line); NEXUS_skip_ws(pos,line); SequenceTree T; string t = strip_NEXUS_comments(line.substr(pos,line.size()-pos)); T.parse(t); leaf_names = T.get_sequences(); std::sort(leaf_names.begin(),leaf_names.end()); return; } catch (std::exception& e) { cerr<<" Error! "<<e.what()<<endl; cerr<<" Quitting read of tree file."<<endl; file->setstate(std::ios::badbit); } } } }
List random_permut(int n) { List L; try { L.resize(n); } catch(std::exception& e) {myexception(e);} for (int i=0; i<n; i++) L[i]=i; std::random_shuffle(L.begin(),L.end()); return L; }
//!ファイル名の変更。失敗した場合は例外を投げる。 void Io::rename( const wchar_t* oldpath, const wchar_t* newpath, bool overwrite) { //oldpathの存在チェック if(!fexist(oldpath))throw myexception(L"rename: oldpath[%s]が存在しません",oldpath); //上書きを許す場合、新しいパスに存在するファイルを削除 if(overwrite)_wremove(newpath); //rename実行 int ret=::_wrename(oldpath,newpath); if(ret!=0){ throw myexception(L"rename(%s,%s) failed.",oldpath,newpath); } }
List random_permut(const List& L) { int n = L.size(); List perm = random_permut(n); List l; try { l.resize(n); } catch(std::exception& e) {myexception(e);} for (int i=0; i<n; i++) l[i] = L[perm[i]]; return l; }
Prune::Prune(const vector<string>& p,const reader_t& r) :wrapped_reader_t(r),prune(p) { vector<string> all_names = tfr->names(); for(int i=0;i<all_names.size();i++) { if (not includes(prune,all_names[i])) leaf_names.push_back(all_names[i]); } for(int i=0;i<prune.size();i++) { int index = find_index(all_names,prune[i]); if (index == -1) throw myexception()<<"Cannot find leaf '"<<prune[i]<<"' in sampled tree."; prune_index.push_back(index); } }
void NEXUS::parse_translate_command(const std::string& s) { translate = true; // cerr<<"translating: "<<line<<endl; vector<string> words = NEXUS_parse_line(s); if (words.size()%3 != 2) throw myexception()<<"Malformed 'TRANSLATE' command: wrong number of tokens."; leaf_names.resize(words.size()/3+1); for(int i=0;i<leaf_names.size();i++) { leaf_names[i] = words[i*3+1]; if (i>0) assert(words[i*3-1] == ","); } }
slide_node_slice_function::slide_node_slice_function(Parameters& P_,int b0) :count(0),P(P_) { vector<const_branchview> b; b.push_back( P.T->directed_branch(b0) ); // choose branches to alter append(b[0].branches_after(),b); if (not b.size() == 3) throw myexception()<<"pointing to leaf node!"; b1 = b[1].undirected_name(); b2 = b[2].undirected_name(); total = P.T->branch(b1).length() + P.T->branch(b2).length(); set_lower_bound(0); set_upper_bound(total); }
void f2(bool flag = true) throw () { if (flag) throw myexception(); }
void f1(bool flag = true) { if (flag) throw myexception(); }
void tr() throw(myexception) { std::cout << "you will dead" << std::endl; throw myexception(); }
int main() { /* Exceptions can jump out of functions. This is their main reason for existing! */ { try { exception_func_int(); } catch (int i) { assert(i == 1); } } /* # std::exception Anything can be thrown, including classes and base types. All stdlib exceptions inherit from `exception`, so it is a good idea to only throw things inherited from it. `std::exception` has limited use since its constructor does not take any arguments, so you cannot describe the error. Some stdlib derived class constructors do however. */ { try { throw std::exception(); } catch (std::exception e) { } } /* Catch blocks work like function overloads and catch by type. */ try { throw 'c'; } catch (int i) { assert(false); } catch (char c) { } /* `...` is the default case */ try { throw 1.0; } catch (int i) { assert(false); } catch (char c) { assert(false); } catch (...) { } /* Derived classes. Just like for function overloading, base classes catch for derived classes. */ { try { throw myexception(); } catch (std::exception& ex) { } /* This compiles, but generates a warning, since the first catch will always catch instead of this one. */ //catch (myexception& ex) {assert(false);} catch (...) {assert(false);} /* this is a more common exception ordering, first derived then base. */ { try { throw myexception(); } catch (myexception& ex) { } catch (std::exception& ex) { assert(false); } catch (...) { assert(false); } } } /* # what Returns a string which contains information about the exception. Many stdlib exceptions simply return the error message given on the constructor. */ { std::string msg = "custom message"; std::ios_base::failure e(msg); // TODO worked in GCC 4.8, failed in GCC 5.1. //assert(e.what() == msg); } /* # uncaught exceptions Uncaught exceptions explose at top level and terminate the program. Check out the error messages generated by each exception. Classes that derive from exception and implement `what()` can print custom messages, which may contain useful debug info. This is a major point in favor of using exception classes instead of base types. */ { //throw 1; //throw 'c'; //throw 1.0; //throw myexception(); } /* # exception specifications Functions can specify which exceptions are catchable with the following syntax. */ { try { exception_func_int_only(true); } catch (int i) { } catch (...) { assert(false); } try { //exception_func_int_only(false); } catch (...) { /* not even ... this can catch non int exceptions thrown by this function */ } try { exception_func_int_exception_only(1); } catch (int i) { } catch (myexception& ex) { } catch (...) { assert(false); } try { //exception_func_none(); } catch (...) { /* no exception thrown by this function is catchable */ } try { //exception_func_none_wrapper(); } catch (...) { /* the same goes if we wrap the function */ } } /* # exception from destructor Never throw an exception from a destructor. Destructors are meant to clean up after exceptions, so if you throw exceptions from them, things get messy. C++ specifies that if this happens during stack unwinding, the program may terminate! What to do to avoid that: <http://stackoverflow.com/questions/130117/throwing-exceptions-out-of-a-destructor> The following code could lead to that. */ if (0) { try { ExceptionDestructor e; } catch (...) { } try { ExceptionDestructorCaller(); } catch (...) { } } #if __cplusplus >= 201103L /* # noexcept Improved version of `throw` for functions. `throw` for functions becomes deprecated in C++11. TODO */ { } #endif }