diff --git a/ChangeLog b/ChangeLog index 0e259c34f1..de06e3b266 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2014-07-24 Ivan Gonzalez + + * lib/khmer.hh, lib/khmer_exception.hh: All exceptions are now derived from + a new base class exception, khmer::khmer_exception. Issue #508. + * lib/counting.cc, lib/hashbits.cc, lib/hashtable.{cc,hh},lib/kmer_hash.cc, + lib/labelhash.cc, lib/perf_metrics.hh, lib/read_parsers.{cc,hh}, + lib/subset.cc, lib/thread_id_map.hh: All exceptions thrown are now + instances (or derived from) khmer::khmer_exception. + 2014-07-22 Jeramia Ory * khmer/_khmermodule.cc: removed unused KhmerError, issue #503 diff --git a/lib/counting.cc b/lib/counting.cc index 30e88136e4..c7d7c165c0 100644 --- a/lib/counting.cc +++ b/lib/counting.cc @@ -107,7 +107,7 @@ CountingHash::abundance_distribution( // if not, could lead to overflow. if (sizeof(BoundedCounterType) != 2) { delete[] dist; - throw std::exception(); + throw khmer_exception(); } while(!parser->is_complete()) { @@ -282,7 +282,7 @@ void CountingHash::get_kadian_count( } if (!counts.size()) { - throw std::exception(); + throw khmer_exception(); } unsigned int kpos = nk * _ksize; @@ -658,7 +658,7 @@ CountingHashFileWriter::CountingHashFileWriter( const CountingHash &ht) { if (!ht._counts[0]) { - throw std::exception(); + throw khmer_exception(); } unsigned int save_ksize = ht._ksize; @@ -711,7 +711,7 @@ CountingHashGzFileWriter::CountingHashGzFileWriter( const CountingHash &ht) { if (!ht._counts[0]) { - throw std::exception(); + throw khmer_exception(); } unsigned int save_ksize = ht._ksize; diff --git a/lib/hashbits.cc b/lib/hashbits.cc index 0e84d3d82b..c0c0268c97 100644 --- a/lib/hashbits.cc +++ b/lib/hashbits.cc @@ -19,7 +19,7 @@ using namespace khmer:: read_parsers; void Hashbits::save(std::string outfilename) { if (!_counts[0]) { - throw std::exception(); + throw khmer_exception(); } unsigned int save_ksize = _ksize; diff --git a/lib/hashtable.cc b/lib/hashtable.cc index 54608ad241..b713a69e74 100644 --- a/lib/hashtable.cc +++ b/lib/hashtable.cc @@ -316,7 +316,7 @@ void Hashtable::get_median_count(const std::string &s, } if (!counts.size()) { - throw std::exception(); + throw khmer_exception(); } if (!counts.size()) { @@ -1159,7 +1159,7 @@ const } if (!(breadth >= cur_breadth)) { // keep track of watermark, for debugging. - throw std::exception(); + throw khmer_exception(); } if (breadth > cur_breadth) { cur_breadth = breadth; @@ -1524,7 +1524,7 @@ const } if (!(breadth >= cur_breadth)) { // keep track of watermark, for debugging. - throw std::exception(); + throw khmer_exception(); } if (breadth > cur_breadth) { cur_breadth = breadth; @@ -1729,7 +1729,7 @@ const } if (!(breadth >= cur_breadth)) { // keep track of watermark, for debugging. - throw std::exception(); + throw khmer_exception(); } if (breadth > cur_breadth) { cur_breadth = breadth; @@ -2036,7 +2036,7 @@ void Hashtable::extract_unique_paths(std::string seq, // then extract. if (!(j == min_length)) { - throw std::exception(); + throw khmer_exception(); } if ( ((float)seen_counter / (float) j) <= max_seen) { unsigned int start = i; diff --git a/lib/hashtable.hh b/lib/hashtable.hh index c811f8f7f8..b865c89ff4 100644 --- a/lib/hashtable.hh +++ b/lib/hashtable.hh @@ -10,7 +10,6 @@ #include #include -#include #include #include @@ -21,6 +20,7 @@ #include #include "khmer.hh" +#include "khmer_exception.hh" #include "read_parsers.hh" #include "subset.hh" #include "kmer_hash.hh" @@ -109,7 +109,7 @@ public: HashIntoType next(HashIntoType& f, HashIntoType& r) { if (done()) { - throw std::exception(); + throw khmer_exception(); } if (!initialized) { @@ -120,7 +120,7 @@ public: unsigned char ch = _seq[index]; index++; if (!(index <= length)) { - throw std::exception(); + throw khmer_exception(); } // left-shift the previous hash over @@ -211,7 +211,7 @@ protected: _ksize( ksize ) { _tag_density = DEFAULT_TAG_DENSITY; if (!(_tag_density % 2 == 0)) { - throw std::exception(); + throw khmer_exception(); } partition = new SubsetPartition(this); _init_bitstuff(); @@ -398,7 +398,7 @@ public: // for debugging/testing purposes only! void _set_tag_density(unsigned int d) { if (!(d % 2 == 0) || !all_tags.empty()) { // must be even and tags must exist - throw std::exception(); + throw khmer_exception(); } _tag_density = d; } diff --git a/lib/khmer.hh b/lib/khmer.hh index 01baa3ac1a..cdb11900f8 100644 --- a/lib/khmer.hh +++ b/lib/khmer.hh @@ -34,12 +34,12 @@ __attribute__((cpychecker_type_object_for_typedef(typename))) #define CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF(typename) #endif -// C++ standard exceptions are subclassed almost ubiquitously. -#include #include #include #include +#include "khmer_exception.hh" + # define MAX_COUNT 255 # define MAX_BIGCOUNT 65535 # define DEFAULT_TAG_DENSITY 40 // must be even @@ -80,10 +80,6 @@ typedef void (*CallbackFn)(const char * info, void * callback_data, unsigned long long n_reads, unsigned long long other); -struct InvalidStreamBuffer : public std:: exception { -}; - - typedef unsigned int PartitionID; typedef std::set SeenSet; typedef std::set PartitionSet; @@ -116,34 +112,6 @@ void deallocate_ptr_set(T& s) } } -class khmer_file_exception : public std::exception -{ -public: - khmer_file_exception(const char * msg) : _msg(msg) { }; - - virtual const char* what() const throw() { - return _msg; - } -protected: - const char * _msg; -}; - -class InvalidStreamHandle : public khmer_file_exception -{ -public: - InvalidStreamHandle() - : khmer_file_exception("Generic InvalidStreamHandle error") {} - InvalidStreamHandle(const char * msg) : khmer_file_exception(msg) {} -}; - -class StreamReadError : public khmer_file_exception -{ -public: - StreamReadError() - : khmer_file_exception("Generic StreamReadError error") {} - StreamReadError(const char * msg) : khmer_file_exception(msg) {} -}; - } #endif // KHMER_HH diff --git a/lib/khmer_exception.hh b/lib/khmer_exception.hh new file mode 100644 index 0000000000..707b9f1243 --- /dev/null +++ b/lib/khmer_exception.hh @@ -0,0 +1,70 @@ +// +// This file is part of khmer, http://github.com/ged-lab/khmer/, and is +// Copyright (C) Michigan State University, 2009-2013. It is licensed under +// the three-clause BSD license; see doc/LICENSE.txt. +// Contact: khmer-project@idyll.org +// + +#ifndef KHMER_EXCEPTION_HH +#define KHMER_EXCEPTION_HH + +#include +#include + +namespace khmer +{ + +/// +// A base class for all exceptions. +// +// All exceptions should be derived from this base class. +// +class khmer_exception : public std::exception +{ +public: + explicit khmer_exception(const char * msg) : _msg(msg) { } + explicit khmer_exception(const std::string& msg = "Generic khmer exception") + : _msg(msg.c_str()) { } + + virtual ~khmer_exception() throw() { } + virtual const char* what() const throw () { return _msg; } + +protected: + const char * _msg; +}; + +/// +// A base class for file exceptions. +// +class khmer_file_exception : public khmer_exception +{ +public: + explicit khmer_file_exception(const char * msg) : khmer_exception(msg) { } + explicit khmer_file_exception(const std::string& msg) + : khmer_exception(msg) { } +}; + +struct InvalidStreamBuffer : public khmer_exception { +}; + +class InvalidStreamHandle : public khmer_file_exception +{ +public: + InvalidStreamHandle() + : khmer_file_exception("Generic InvalidStreamHandle error") {} + InvalidStreamHandle(const char * msg) : khmer_file_exception(msg) {} +}; + +class StreamReadError : public khmer_file_exception +{ +public: + StreamReadError() + : khmer_file_exception("Generic StreamReadError error") {} + StreamReadError(const char * msg) : khmer_file_exception(msg) {} +}; + +} + +#endif // KHMER_EXCEPTION_HH + +// vim: set sts=2 sw=2: diff --git a/lib/kmer_hash.cc b/lib/kmer_hash.cc index ba02317d8d..f79de33e83 100644 --- a/lib/kmer_hash.cc +++ b/lib/kmer_hash.cc @@ -28,7 +28,7 @@ HashIntoType _hash(const char * kmer, const WordLength k, { // sizeof(HashIntoType) * 8 bits / 2 bits/base if (!(k <= sizeof(HashIntoType)*4) || !(strlen(kmer) >= k)) { - throw std::exception(); + throw khmer_exception(); } HashIntoType h = 0, r = 0; diff --git a/lib/labelhash.cc b/lib/labelhash.cc index b08c07cb6f..6851423e58 100644 --- a/lib/labelhash.cc +++ b/lib/labelhash.cc @@ -320,7 +320,7 @@ unsigned int LabelHash::sweep_label_neighborhood(const std::string& seq, //printf("range=%u ", range); if (range == 0) { if (!(num_traversed == seq.length()-ksize()+1)) { - throw std::exception(); + throw khmer_exception(); } } tagged_kmers.clear(); diff --git a/lib/perf_metrics.hh b/lib/perf_metrics.hh index c4a377b9a1..aecd21d879 100644 --- a/lib/perf_metrics.hh +++ b/lib/perf_metrics.hh @@ -12,16 +12,14 @@ #include #include -#include #include "khmer.hh" - namespace khmer { #ifdef WITH_INTERNAL_METRICS -struct InvalidPerformanceMetricsKey : public std:: exception { +struct InvalidPerformanceMetricsKey : public khmer_exception { }; diff --git a/lib/read_parsers.cc b/lib/read_parsers.cc index 3e8845c2ee..f569b25211 100644 --- a/lib/read_parsers.cc +++ b/lib/read_parsers.cc @@ -1411,7 +1411,7 @@ IParser( REG_EXTENDED | REG_NOSUB ); if (regex_rc) { - throw std::exception(); + throw khmer_exception(); } regex_rc = regcomp( @@ -1419,7 +1419,7 @@ IParser( "^.+(/1| 1:[YN]:[[:digit:]]+:[[:alpha:]]+).{0}", REG_EXTENDED ); if (regex_rc) { - throw std::exception(); + throw khmer_exception(); } regex_rc = regcomp( @@ -1427,7 +1427,7 @@ IParser( "^.+(/2| 2:[YN]:[[:digit:]]+:[[:alpha:]]+).{0}", REG_EXTENDED ); if (regex_rc) { - throw std::exception(); + throw khmer_exception(); } } diff --git a/lib/read_parsers.hh b/lib/read_parsers.hh index cba6bef8da..6668c87e7f 100644 --- a/lib/read_parsers.hh +++ b/lib/read_parsers.hh @@ -84,22 +84,22 @@ public: }; -struct CacheSegmentUnavailable : public std:: exception { +struct CacheSegmentUnavailable : public khmer_exception { }; -struct CacheSegmentBoundaryViolation : public std:: exception { +struct CacheSegmentBoundaryViolation : public khmer_exception { }; -struct InvalidCacheSizeRequested : public std:: exception { +struct InvalidCacheSizeRequested : public khmer_exception { }; -struct NoMoreReadsAvailable : public std:: exception { +struct NoMoreReadsAvailable : public khmer_exception { }; -struct UnknownPairReadingMode : public std:: exception { +struct UnknownPairReadingMode : public khmer_exception { }; -struct InvalidReadPair : public std:: exception { +struct InvalidReadPair : public khmer_exception { }; #ifdef WITH_INTERNAL_METRICS diff --git a/lib/subset.cc b/lib/subset.cc index 04d2c3daf2..df7819d73d 100644 --- a/lib/subset.cc +++ b/lib/subset.cc @@ -144,7 +144,7 @@ size_t SubsetPartition::output_partitioned_file( #ifdef VALIDATE_PARTITIONS std::cout << "checking: " << read.name << "\n"; if (!is_single_partition(seq)) { - throw std::exception(); + throw khmer_exception(); } #endif // VALIDATE_PARTITIONS @@ -478,7 +478,7 @@ void SubsetPartition::find_all_tags( if (!(breadth >= cur_breadth)) { // keep track of watermark, for // debugging. - throw std::exception(); + throw khmer_exception(); } if (breadth > cur_breadth) { cur_breadth = breadth; @@ -766,7 +766,7 @@ void SubsetPartition::find_all_tags_truncate_on_abundance( // @cswelcher Do these lines actually do anything? if (!(breadth >= cur_breadth)) { // keep track of watermark, for // debugging. - throw std::exception(); + throw khmer_exception(); } if (breadth > cur_breadth) { cur_breadth = breadth; @@ -991,7 +991,7 @@ void SubsetPartition::set_partition_id( { HashIntoType kmer; if (!(kmer_s.length() >= _ht->ksize())) { - throw std::exception(); + throw khmer_exception(); } kmer = _hash(kmer_s.c_str(), _ht->ksize()); @@ -1161,7 +1161,7 @@ PartitionID SubsetPartition::get_partition_id(std::string kmer_s) { HashIntoType kmer; if (!(kmer_s.length() >= _ht->ksize())) { - throw std::exception(); + throw khmer_exception(); } kmer = _hash(kmer_s.c_str(), _ht->ksize()); @@ -1351,7 +1351,7 @@ void SubsetPartition::merge_from_disk(string other_filename) i += sizeof(PartitionID); if (!(*diskp != 0)) { // sanity check. - throw std::exception(); + throw khmer_exception(); } _merge_other(*kmer_p, *diskp, diskp_to_pp); @@ -1359,7 +1359,7 @@ void SubsetPartition::merge_from_disk(string other_filename) loaded++; } if (!(i == n_bytes)) { - throw std::exception(); + throw khmer_exception(); } memcpy(buf, buf + n_bytes, remainder); } @@ -1442,7 +1442,7 @@ void SubsetPartition::_validate_pmap() if (pp_id != NULL) { if (!(*pp_id >= 1) || !(*pp_id < next_partition_id)) { - throw std::exception(); + throw khmer_exception(); } } } @@ -1453,7 +1453,7 @@ void SubsetPartition::_validate_pmap() PartitionPtrSet *s = (*ri).second; if (!(s != NULL)) { - throw std::exception(); + throw khmer_exception(); } for (PartitionPtrSet::const_iterator si = s->begin(); si != s->end(); @@ -1462,7 +1462,7 @@ void SubsetPartition::_validate_pmap() pp = *si; if (!(p == *pp)) { - throw std::exception(); + throw khmer_exception(); } } } @@ -1626,7 +1626,7 @@ unsigned long long SubsetPartition::repartition_largest_partition( --di; if (d.empty()) { - throw std::exception(); + throw khmer_exception(); } for (PartitionCountMap::const_iterator cmi = cm.begin(); cmi != cm.end(); @@ -1636,7 +1636,7 @@ unsigned long long SubsetPartition::repartition_largest_partition( } } if (!(biggest_p != 0)) { - throw std::exception(); + throw khmer_exception(); } #if VERBOSE_REPARTITION diff --git a/lib/thread_id_map.hh b/lib/thread_id_map.hh index b1e718d95a..4d3d7f3124 100644 --- a/lib/thread_id_map.hh +++ b/lib/thread_id_map.hh @@ -8,7 +8,6 @@ #ifndef THREAD_ID_MAP_HH # define THREAD_ID_MAP_HH -#include #include // TODO? Just use 'pthread_t' everywhere. @@ -31,10 +30,10 @@ namespace khmer { -struct InvalidNumberOfThreadsRequested : public std:: exception { +struct InvalidNumberOfThreadsRequested : public khmer_exception { }; -struct TooManyThreads : public std:: exception { +struct TooManyThreads : public khmer_exception { };