From: Mikko Rasa Date: Tue, 6 Dec 2022 23:02:22 +0000 (+0200) Subject: Add new exception classes for some common errors X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=commitdiff_plain;h=1787d4928ac1285f5434a2c8d0676deea9ce9176 Add new exception classes for some common errors --- diff --git a/source/core/application.cpp b/source/core/application.cpp index 8447eed..1b77a7c 100644 --- a/source/core/application.cpp +++ b/source/core/application.cpp @@ -9,6 +9,7 @@ #include #include #include "application.h" +#include "except.h" #include "getopt.h" using namespace std; @@ -24,7 +25,7 @@ void *Application::_data = nullptr; Application::Application(const string &n) { if(_app) - throw logic_error("instance already exists"); + throw already_called("Application::Application"); if(!n.empty()) _name = n; @@ -94,7 +95,7 @@ int Application::run(int argc, char **argv, void *data, void (*created_callback) void Application::set_startup_info(const char *argv0, void *data) { if(_argv0) - throw logic_error("startup info already set"); + throw already_called("Application::set_startup_info"); static FS::Path exe; @@ -146,7 +147,7 @@ void Application::_sighandler(int s) Application::Starter::Starter() { if(_starter) - throw logic_error("Can't create more than one Starter instance"); + throw already_called("Application::Starter::Starter"); _starter = this; } diff --git a/source/core/except.h b/source/core/except.h new file mode 100644 index 0000000..65d9ef9 --- /dev/null +++ b/source/core/except.h @@ -0,0 +1,37 @@ +#ifndef MSP_CORE_EXCEPT_H_ +#define MSP_CORE_EXCEPT_H_ + +#include + +namespace Msp { + +class invalid_state: public std::logic_error +{ +public: + invalid_state(const std::string &w): logic_error(w) { } +}; + + +class already_called: public invalid_state +{ +public: + already_called(const std::string &w): invalid_state(w) { } +}; + + +class unsupported: public std::logic_error +{ +public: + unsupported(const std::string &w): logic_error(w) { } +}; + + +class internal_error: public std::logic_error +{ +public: + internal_error(const std::string &w): logic_error(w) { } +}; + +} // namespace Msp; + +#endif diff --git a/source/core/process.cpp b/source/core/process.cpp index 43b7384..4c5ed46 100644 --- a/source/core/process.cpp +++ b/source/core/process.cpp @@ -1,4 +1,5 @@ #include +#include "except.h" #include "process.h" #include "process_private.h" @@ -78,7 +79,7 @@ void Process::execute(const FS::Path &command, const Arguments &args) unsigned Process::get_exit_code() const { if(!finished) - throw logic_error("not finished"); + throw invalid_state("not finished"); return exit_code; } diff --git a/source/core/thread.cpp b/source/core/thread.cpp index 841cc3f..ad78d94 100644 --- a/source/core/thread.cpp +++ b/source/core/thread.cpp @@ -1,4 +1,4 @@ -#include +#include "except.h" #include "thread.h" #include "thread_private.h" @@ -39,7 +39,7 @@ void Thread::kill() void Thread::launch() { if(_state>=RUNNING) - throw logic_error("already launched"); + throw already_called("Thread::launch"); platform_launch(); _state = RUNNING; diff --git a/source/core/unix/process.cpp b/source/core/unix/process.cpp index 7370b70..975fa22 100644 --- a/source/core/unix/process.cpp +++ b/source/core/unix/process.cpp @@ -4,6 +4,7 @@ #include #include #include +#include "except.h" #include "process.h" #include "process_private.h" @@ -70,7 +71,7 @@ void Process::execute(const string &command, bool path_search, const Arguments & bool Process::wait(bool block) { if(!running) - throw logic_error("not running"); + throw invalid_state("not running"); int status; int pid = waitpid(priv->info.pid, &status, (block ? 0 : WNOHANG)); diff --git a/source/core/windows/process.cpp b/source/core/windows/process.cpp index 3bd03fa..aa0fad9 100644 --- a/source/core/windows/process.cpp +++ b/source/core/windows/process.cpp @@ -2,6 +2,7 @@ #include #include #include +#include "except.h" #include "process.h" #include "process_private.h" @@ -99,7 +100,7 @@ void Process::execute(const string &command, bool path_search, const Arguments & bool Process::wait(bool block) { if(!running) - throw logic_error("not running"); + throw invalid_state("not running"); DWORD ret = WaitForSingleObject(priv->info.hProcess, (block ? INFINITE : 0)); if(ret==WAIT_FAILED) diff --git a/source/fs/dir.cpp b/source/fs/dir.cpp index 066d88b..0f617e5 100644 --- a/source/fs/dir.cpp +++ b/source/fs/dir.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include "dir.h" #include "path.h" @@ -109,7 +110,7 @@ Path get_sys_conf_dir() { const char *argv0 = Application::get_argv0(); if(!argv0) - throw logic_error("no startup command"); + throw invalid_state("no startup command"); Path dir = get_bin_dir(argv0); @@ -128,7 +129,7 @@ Path get_sys_data_dir() { const char *argv0 = Application::get_argv0(); if(!argv0) - throw logic_error("no startup command"); + throw invalid_state("no startup command"); Path dir = get_bin_dir(argv0); @@ -144,7 +145,7 @@ Path get_sys_lib_dir() { const char *argv0 = Application::get_argv0(); if(!argv0) - throw logic_error("no startup command"); + throw invalid_state("no startup command"); Path dir = get_bin_dir(argv0); diff --git a/source/fs/filemonitor.cpp b/source/fs/filemonitor.cpp index 5ab3f49..5ff6392 100644 --- a/source/fs/filemonitor.cpp +++ b/source/fs/filemonitor.cpp @@ -1,4 +1,5 @@ #include +#include #include "filemonitor.h" #include "filemonitor_platform.h" @@ -19,7 +20,7 @@ FileMonitor::~FileMonitor() void FileMonitor::use_event_dispatcher(IO::EventDispatcher &ed) { if(event_disp) - throw logic_error("event_disp!=nullptr"); + throw already_called("FileMonitor::use_event_dispatcher"); event_disp = &ed; platform_use_event_dispatcher(); diff --git a/source/fs/osx/dir_location.cpp b/source/fs/osx/dir_location.cpp index b58b971..13aa3d2 100644 --- a/source/fs/osx/dir_location.cpp +++ b/source/fs/osx/dir_location.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "dir.h" using namespace std; @@ -28,7 +29,7 @@ Path get_user_data_dir() { const string &appname = Application::get_name(); if(appname.empty()) - throw logic_error("no application name"); + throw invalid_state("no application name"); char buf[1024]; unsigned len = get_application_support_dir(buf, sizeof(buf)); diff --git a/source/fs/unix/dir_location.cpp b/source/fs/unix/dir_location.cpp index 7382587..68d82a2 100644 --- a/source/fs/unix/dir_location.cpp +++ b/source/fs/unix/dir_location.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "dir.h" using namespace std; @@ -19,7 +20,7 @@ Path get_user_data_dir() { const string &appname = Application::get_name(); if(appname.empty()) - throw logic_error("no application name"); + throw invalid_state("no application name"); return get_home_dir()/("."+appname); } diff --git a/source/fs/windows/dir_location.cpp b/source/fs/windows/dir_location.cpp index 4f9b8f6..85c5c2f 100644 --- a/source/fs/windows/dir_location.cpp +++ b/source/fs/windows/dir_location.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "dir.h" using namespace std; @@ -19,7 +20,7 @@ Path get_user_data_dir() { const string &appname = Application::get_name(); if(appname.empty()) - throw logic_error("no application name"); + throw invalid_state("no application name"); char datadir[MAX_PATH]; if(SHGetFolderPath(nullptr, CSIDL_LOCAL_APPDATA, nullptr, 0, datadir)==S_OK) diff --git a/source/fs/windows/filemonitor.cpp b/source/fs/windows/filemonitor.cpp index 3ed65b1..da0bca3 100644 --- a/source/fs/windows/filemonitor.cpp +++ b/source/fs/windows/filemonitor.cpp @@ -21,7 +21,7 @@ void FileMonitor::tick() FileMonitor::Private::Private(FileMonitor &) { - throw logic_error("not implemented"); + throw unsupported("FileMonitor"); } } // namespace FS diff --git a/source/fs/windows/utils.cpp b/source/fs/windows/utils.cpp index ca9e7aa..66699aa 100644 --- a/source/fs/windows/utils.cpp +++ b/source/fs/windows/utils.cpp @@ -11,7 +11,7 @@ namespace FS { Path readlink(const Path &link) { (void)link; - throw logic_error("no symbolic links on win32"); + throw unsupported("no symbolic links on win32"); } Path realpath(const Path &path) diff --git a/source/io/asset.cpp b/source/io/asset.cpp index 9284f97..d605ff4 100644 --- a/source/io/asset.cpp +++ b/source/io/asset.cpp @@ -1,3 +1,4 @@ +#include #include "asset.h" using namespace std; @@ -7,12 +8,12 @@ namespace IO { void Asset::set_block(bool) { - throw logic_error("Asset::set_block"); + throw unsupported("Asset::set_block"); } void Asset::set_inherit(bool) { - throw logic_error("Asset::set_inherit"); + throw unsupported("Asset::set_inherit"); } size_t Asset::do_write(const char *, size_t) @@ -22,7 +23,7 @@ size_t Asset::do_write(const char *, size_t) const Handle &Asset::get_handle(Mode) { - throw logic_error("Asset::get_handle"); + throw unsupported("Asset::get_handle"); } } // namespace IO diff --git a/source/io/buffered.cpp b/source/io/buffered.cpp index 638e4ec..9f001fb 100644 --- a/source/io/buffered.cpp +++ b/source/io/buffered.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include "buffered.h" #include "handle.h" @@ -33,12 +33,12 @@ Buffered::~Buffered() void Buffered::set_block(bool) { - throw logic_error("Buffered::set_block"); + throw unsupported("Buffered::set_block"); } void Buffered::set_inherit(bool) { - throw logic_error("Buffered::set_block"); + throw unsupported("Buffered::set_block"); } void Buffered::flush() @@ -179,7 +179,7 @@ int Buffered::get() const Handle &Buffered::get_handle(Mode) { - throw logic_error("Buffered::get_handle"); + throw unsupported("Buffered::get_handle"); } void Buffered::set_op(Mode op) diff --git a/source/io/file.cpp b/source/io/file.cpp index 5ead278..7d6e1f9 100644 --- a/source/io/file.cpp +++ b/source/io/file.cpp @@ -1,3 +1,4 @@ +#include #include "file.h" #include "handle_private.h" @@ -104,7 +105,7 @@ void BufferedFile::set_block(bool b) void BufferedFile::set_inherit(bool) { - throw logic_error("BufferedFile::set_inherit"); + throw unsupported("BufferedFile::set_inherit"); } size_t BufferedFile::do_write(const char *buf, size_t size) @@ -146,7 +147,7 @@ int BufferedFile::get() const Handle &BufferedFile::get_handle(Mode) { - throw logic_error("BufferedFile::get_handle"); + throw unsupported("BufferedFile::get_handle"); } SeekOffset BufferedFile::seek(SeekOffset offset, SeekType type) diff --git a/source/io/memory.cpp b/source/io/memory.cpp index 0b2d8f4..19629c4 100644 --- a/source/io/memory.cpp +++ b/source/io/memory.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "handle.h" #include "memory.h" @@ -18,12 +19,12 @@ Memory::Memory(char *b, char *e, Mode m) void Memory::set_block(bool) { - throw logic_error("Memory::set_block"); + throw unsupported("Memory::set_block"); } void Memory::set_inherit(bool) { - throw logic_error("Memory::set_inherit"); + throw unsupported("Memory::set_inherit"); } size_t Memory::do_write(const char *buf, size_t size) @@ -94,7 +95,7 @@ int Memory::get() const Handle &Memory::get_handle(Mode) { - throw logic_error("Memory::get_handle"); + throw unsupported("Memory::get_handle"); } SeekOffset Memory::seek(SeekOffset off, SeekType type) diff --git a/source/io/poll.cpp b/source/io/poll.cpp index 4f9b24f..cfaa57b 100644 --- a/source/io/poll.cpp +++ b/source/io/poll.cpp @@ -44,7 +44,7 @@ void Poller::set_object(EventObject &obj, PollEvent ev) #ifdef _WIN32 if(objects.size()>=MAXIMUM_WAIT_OBJECTS) - throw logic_error("Maximum number of wait objects reached"); + throw invalid_state("Maximum number of wait objects reached"); #endif objects.push_back(PolledObject(&obj, ev)); diff --git a/source/io/slice.cpp b/source/io/slice.cpp index 535deae..98013b6 100644 --- a/source/io/slice.cpp +++ b/source/io/slice.cpp @@ -1,4 +1,4 @@ -#include +#include #include "slice.h" using namespace std; @@ -21,12 +21,12 @@ Slice::Slice(Seekable &b, SeekOffset s, SeekOffset l): void Slice::set_block(bool) { - throw logic_error("Slice::set_block"); + throw unsupported("Slice::set_block"); } void Slice::set_inherit(bool) { - throw logic_error("Slice::set_inherit"); + throw unsupported("Slice::set_inherit"); } void Slice::flush() @@ -108,7 +108,7 @@ int Slice::get() const Handle &Slice::get_handle(Mode) { - throw logic_error("Slice::get_handle"); + throw unsupported("Slice::get_handle"); } SeekOffset Slice::seek(SeekOffset off, SeekType type) diff --git a/source/io/windows/console.cpp b/source/io/windows/console.cpp index a331133..5d68c23 100644 --- a/source/io/windows/console.cpp +++ b/source/io/windows/console.cpp @@ -13,7 +13,7 @@ DWORD stream_to_sys(Msp::IO::Console::Stream stream) case Msp::IO::Console::CIN: return STD_INPUT_HANDLE; case Msp::IO::Console::COUT: return STD_OUTPUT_HANDLE; case Msp::IO::Console::CERR: return STD_ERROR_HANDLE; - default: throw invalid_argument("stream_to_sys"); + default: throw internal_error("stream_to_sys"); } } diff --git a/source/io/zlibcompressed.cpp b/source/io/zlibcompressed.cpp index fba71eb..8896f14 100644 --- a/source/io/zlibcompressed.cpp +++ b/source/io/zlibcompressed.cpp @@ -1,6 +1,7 @@ #ifdef WITH_ZLIB #include #endif +#include #include "zlibcompressed.h" using namespace std; @@ -73,7 +74,7 @@ ZlibCompressed::ZlibCompressed(Base &b, Mode m, unsigned level): (void)buffer_size; (void)stream_end; (void)level; - throw zlib_error("unsupported", -1); + throw unsupported("ZlibCompressed"); #endif } @@ -96,12 +97,12 @@ ZlibCompressed::~ZlibCompressed() void ZlibCompressed::set_block(bool) { - throw logic_error("ZlibCompressed::set_block"); + throw unsupported("ZlibCompressed::set_block"); } void ZlibCompressed::set_inherit(bool) { - throw logic_error("ZlibCompressed::set_inherit"); + throw unsupported("ZlibCompressed::set_inherit"); } void ZlibCompressed::flush() @@ -261,7 +262,7 @@ size_t ZlibCompressed::do_read(char *data, size_t size) const Handle &ZlibCompressed::get_handle(Mode) { - throw logic_error("ZlibCompressed::get_handle"); + throw unsupported("ZlibCompressed::get_handle"); } } // namespace IO diff --git a/source/stringcodec/iso2022jp.cpp b/source/stringcodec/iso2022jp.cpp index 3332922..93c50f4 100644 --- a/source/stringcodec/iso2022jp.cpp +++ b/source/stringcodec/iso2022jp.cpp @@ -1,3 +1,4 @@ +#include #include "ascii.h" #include "iso2022jp.h" #include "jisx0201.h" @@ -122,7 +123,7 @@ unichar Iso2022Jp::Decoder::decode_char(const string &str, string::const_iterato else if(dec) return dec->decode_char(str, i); else - throw logic_error("no sub-decoder"); + throw internal_error("no sub-decoder"); if(result>=0) return result; diff --git a/source/strings/regex.cpp b/source/strings/regex.cpp index 915d335..5307c69 100644 --- a/source/strings/regex.cpp +++ b/source/strings/regex.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include "format.h" #include "regex.h" @@ -477,7 +478,7 @@ bool Regex::run(const string &str, const string::const_iterator &begin, vector