Skip to content

Commit 9886ab6

Browse files
committed
using xtd::helpers::throw_helper class
1 parent 68aff95 commit 9886ab6

File tree

7 files changed

+51
-26
lines changed

7 files changed

+51
-26
lines changed

src/xtd.core/include/xtd/helpers/exception_case.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ namespace xtd {
3636
argument_null,
3737
/// @brief The argument is out of range.
3838
argument_out_of_range,
39+
/// @brief The arithmetic operation is not valid.
40+
arithmetic,
3941
/// @brief The post-phase action of a xtd::threading::barrier fails.
4042
barrier_post_phase,
4143
/// @brief The directory is not found.
@@ -88,6 +90,12 @@ namespace xtd {
8890
semaphore_full,
8991
/// @brief The lock is not valid.
9092
synchronization_lock,
93+
/// @brief The thread is abort.
94+
thread_abort,
95+
/// @brief The thread is interrupted.
96+
thread_interrupted,
97+
/// @brief The thread state is not valid.
98+
thread_state,
9199
/// @brief The access is denied.
92100
unauthorized_access,
93101
};

src/xtd.core/src/xtd/console.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
using namespace xtd;
1717
using namespace xtd::collections::generic;
18+
using namespace xtd::helpers;
1819
using namespace xtd::io;
1920

2021
namespace {
@@ -147,7 +148,7 @@ int32 console::input_code_page() {
147148

148149
void console::input_code_page(int32 code_page) {
149150
register_cancel_key_press(); // Must be first...
150-
if (!native::console::input_code_page(code_page)) throw io::io_exception {};
151+
if (!native::console::input_code_page(code_page)) throw_helper::throws(exception_case::io);
151152
}
152153

153154
bool console::is_error_redirected() {
@@ -192,7 +193,7 @@ int32 console::output_code_page() {
192193

193194
void console::output_code_page(int32 code_page) {
194195
register_cancel_key_press(); // Must be first...
195-
if (!native::console::output_code_page(code_page)) throw io::io_exception {};
196+
if (!native::console::output_code_page(code_page)) throw_helper::throws(exception_case::io);
196197
}
197198

198199
string console::title() {

src/xtd.core/src/xtd/date_time.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <tuple>
1515

1616
using namespace xtd;
17+
using namespace xtd::helpers;
1718

1819
namespace {
1920
// Number of ticks per time unit
@@ -457,7 +458,7 @@ string date_time::to_string(const string& format) const {
457458
string date_time::to_string(const string& format, const std::locale& loc) const {
458459
auto fmt = format;
459460
if (fmt.empty()) fmt = "G";
460-
if (fmt.size() > 1) throw format_exception("Invalid format"_t);
461+
if (fmt.size() > 1) throw_helper::throws(exception_case::format, "Invalid format"_t);
461462

462463
[[maybe_unused]] auto [year, month, day, hour, minute, second, day_of_year, day_of_week] = get_date_time();
463464
switch (fmt[0]) {

src/xtd.core/src/xtd/environment.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ struct __update__macos_path__ {
3434
} __updmacpath__;
3535

3636
using namespace xtd;
37+
using namespace xtd::helpers;
3738
using namespace xtd::io;
3839
using namespace xtd::threading;
3940

@@ -70,7 +71,7 @@ class environment::signal_catcher {
7071
std::signal(signal, signal_catcher::on_abnormal_termination_occured);
7172
auto e = signal_cancel_event_args {xtd::signal::abnormal_termination};
7273
environment::on_cancel_signal(e);
73-
if (!e.cancel()) environment::quick_exit(128 + last_signal_.value_or(signal)); //throw thread_abort_exception {};
74+
if (!e.cancel()) environment::quick_exit(128 + last_signal_.value_or(signal)); //throw_helper::throws(exception_case::thread_abort);
7475
}
7576

7677
static void on_floating_point_exception_occured(int32 signal) {
@@ -79,7 +80,7 @@ class environment::signal_catcher {
7980
environment::on_cancel_signal(e);
8081
if (!e.cancel()) {
8182
last_signal_ = signal;
82-
throw arithmetic_exception {};
83+
throw_helper::throws(exception_case::arithmetic);
8384
}
8485
}
8586

@@ -89,7 +90,7 @@ class environment::signal_catcher {
8990
environment::on_cancel_signal(e);
9091
if (!e.cancel()) {
9192
last_signal_ = signal;
92-
xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation);
93+
throw_helper::throws(exception_case::invalid_operation);
9394
}
9495
}
9596

@@ -334,7 +335,7 @@ xtd::string environment::get_environment_variable(const xtd::string& variable) {
334335
}
335336

336337
string environment::get_environment_variable(const string& variable, environment_variable_target target) {
337-
if (!enum_object<>::is_defined<environment_variable_target>(target)) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::argument, "Invalid environment variable target value"_t);
338+
if (!enum_object<>::is_defined<environment_variable_target>(target)) throw_helper::throws(exception_case::argument, "Invalid environment variable target value"_t);
338339
return native::environment::get_environment_variable(variable, as<int32>(target));
339340
}
340341

@@ -343,7 +344,7 @@ std::map<std::string, std::string>& environment::get_environment_variables() {
343344
}
344345

345346
std::map<std::string, std::string>& environment::get_environment_variables(environment_variable_target target) {
346-
if (!enum_object<>::is_defined<environment_variable_target>(target)) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::argument, "Invalid environment variable target value"_t);
347+
if (!enum_object<>::is_defined<environment_variable_target>(target)) throw_helper::throws(exception_case::argument, "Invalid environment variable target value"_t);
347348
return native::environment::get_environment_variables(as<int32>(target));
348349
}
349350

@@ -374,9 +375,9 @@ void environment::set_environment_variable(const xtd::string& variable, const xt
374375
}
375376

376377
void environment::set_environment_variable(const string& variable, const string& value, environment_variable_target target) {
377-
if (string::is_empty(variable)) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::argument, "Environment variable name is empty"_t);
378+
if (string::is_empty(variable)) throw_helper::throws(exception_case::argument, "Environment variable name is empty"_t);
378379

379-
if (!enum_object<>::is_defined<environment_variable_target>(target)) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::argument, "Invalid environment variable target value"_t);
380+
if (!enum_object<>::is_defined<environment_variable_target>(target)) throw_helper::throws(exception_case::argument, "Invalid environment variable target value"_t);
380381

381382
if (string::is_empty(value)) {
382383
native::environment::get_environment_variables(as<int32>(target)).erase(variable);

src/xtd.core/src/xtd/helpers/throw_helper.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@
1313
#include "../../../include/xtd/threading/lock_recursion_exception.hpp"
1414
#include "../../../include/xtd/threading/semaphore_full_exception.hpp"
1515
#include "../../../include/xtd/threading/synchronization_lock_exception.hpp"
16+
#include "../../../include/xtd/threading/thread_abort_exception.hpp"
17+
#include "../../../include/xtd/threading/thread_interrupted_exception.hpp"
18+
#include "../../../include/xtd/threading/thread_state_exception.hpp"
1619
#include "../../../include/xtd/argument_exception.hpp"
1720
#include "../../../include/xtd/argument_null_exception.hpp"
1821
#include "../../../include/xtd/argument_out_of_range_exception.hpp"
22+
#include "../../../include/xtd/arithmetic_exception.hpp"
1923
#include "../../../include/xtd/format_exception.hpp"
2024
#include "../../../include/xtd/index_out_of_range_exception.hpp"
2125
#include "../../../include/xtd/invalid_cast_exception.hpp"
@@ -50,6 +54,7 @@ void throw_helper::throws(enum exception_case exception_case, const source_locat
5054
case exception_case::argument: throw argument_exception {to_stack_frame(location)};
5155
case exception_case::argument_null: throw argument_null_exception {to_stack_frame(location)};
5256
case exception_case::argument_out_of_range: throw argument_out_of_range_exception {to_stack_frame(location)};
57+
case exception_case::arithmetic: throw arithmetic_exception {to_stack_frame(location)};
5358
case exception_case::barrier_post_phase: throw barrier_post_phase_exception {to_stack_frame(location)};
5459
case exception_case::directory_not_found: throw directory_not_found_exception {to_stack_frame(location)};
5560
case exception_case::end_of_stream: throw end_of_stream_exception {to_stack_frame(location)};
@@ -76,6 +81,9 @@ void throw_helper::throws(enum exception_case exception_case, const source_locat
7681
case exception_case::rank: throw rank_exception {to_stack_frame(location)};
7782
case exception_case::semaphore_full: throw semaphore_full_exception {to_stack_frame(location)};
7883
case exception_case::synchronization_lock: throw synchronization_lock_exception {to_stack_frame(location)};
84+
case exception_case::thread_abort: throw thread_abort_exception {to_stack_frame(location)};
85+
case exception_case::thread_interrupted: throw thread_interrupted_exception {to_stack_frame(location)};
86+
case exception_case::thread_state: throw thread_state_exception {to_stack_frame(location)};
7987
case exception_case::unauthorized_access: throw unauthorized_access_exception {to_stack_frame(location)};
8088
default: throw argument_exception {"Invalid xtd::helpers::exception_case value"};
8189
}
@@ -88,6 +96,7 @@ void throw_helper::throws(enum exception_case exception_case, const char* messag
8896
case exception_case::argument: throw argument_exception {message, to_stack_frame(location)};
8997
case exception_case::argument_null: throw argument_null_exception {message, to_stack_frame(location)};
9098
case exception_case::argument_out_of_range: throw argument_out_of_range_exception {message, to_stack_frame(location)};
99+
case exception_case::arithmetic: throw arithmetic_exception {message, to_stack_frame(location)};
91100
case exception_case::barrier_post_phase: throw barrier_post_phase_exception {message, to_stack_frame(location)};
92101
case exception_case::directory_not_found: throw directory_not_found_exception {message, to_stack_frame(location)};
93102
case exception_case::end_of_stream: throw end_of_stream_exception {message, to_stack_frame(location)};
@@ -114,6 +123,9 @@ void throw_helper::throws(enum exception_case exception_case, const char* messag
114123
case exception_case::rank: throw rank_exception {message, to_stack_frame(location)};
115124
case exception_case::semaphore_full: throw semaphore_full_exception {message, to_stack_frame(location)};
116125
case exception_case::synchronization_lock: throw synchronization_lock_exception {message, to_stack_frame(location)};
126+
case exception_case::thread_abort: throw thread_abort_exception {message, to_stack_frame(location)};
127+
case exception_case::thread_interrupted: throw thread_interrupted_exception {message, to_stack_frame(location)};
128+
case exception_case::thread_state: throw thread_state_exception {message, to_stack_frame(location)};
117129
case exception_case::unauthorized_access: throw unauthorized_access_exception {message, to_stack_frame(location)};
118130
default: throw argument_exception {"Invalid xtd::helpers::exception_case value"};
119131
}

src/xtd.core/src/xtd/threading/thread.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
using namespace xtd;
2626
using namespace xtd::diagnostics;
27+
using namespace xtd::helpers;
2728
using namespace xtd::threading;
2829

2930
struct thread::data {
@@ -107,7 +108,7 @@ bool thread::auto_join() const noexcept {
107108
}
108109

109110
thread& thread::auto_join(bool value) {
110-
if (is_aborted() || is_stopped()) throw thread_state_exception {};
111+
if (is_aborted() || is_stopped()) throw_helper::throws(exception_case::thread_state);
111112
data_->auto_join = value;
112113
return *this;
113114
}
@@ -178,7 +179,7 @@ xtd::threading::thread_priority thread::priority() const noexcept {
178179

179180
thread& thread::priority(xtd::threading::thread_priority value) {
180181
if (!enum_object<>::is_defined(value)) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::argument);
181-
if (is_aborted() || is_stopped()) throw thread_state_exception {};
182+
if (is_aborted() || is_stopped()) throw_helper::throws(exception_case::thread_state);
182183

183184
if (data_->priority == value) return *this;
184185
data_->priority = value;
@@ -195,7 +196,7 @@ xtd::threading::thread_state thread::thread_state() const noexcept {
195196
}
196197

197198
void thread::abort() {
198-
if (is_unstarted() || is_suspended()) throw thread_state_exception {};
199+
if (is_unstarted() || is_suspended()) throw_helper::throws(exception_case::thread_state);
199200

200201
data_->state |= xtd::threading::thread_state::abort_requested;
201202

@@ -230,7 +231,7 @@ void thread::join() {
230231
}
231232

232233
bool thread::join(int32 milliseconds_timeout) {
233-
if (is_unstarted()) throw thread_state_exception {};
234+
if (is_unstarted()) throw_helper::throws(exception_case::thread_state);
234235
if (milliseconds_timeout < timeout::infinite) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::argument);
235236

236237
if (data_->interrupted == true) interrupt_internal();
@@ -282,7 +283,7 @@ bool thread::join_all(const time_span& timeout) {
282283
}
283284

284285
void thread::resume() {
285-
if (is_unstarted() || !is_suspended()) throw thread_state_exception {};
286+
if (is_unstarted() || !is_suspended()) throw_helper::throws(exception_case::thread_state);
286287

287288
native::thread::resume(data_->handle);
288289
data_->state &= ~xtd::threading::thread_state::suspended;
@@ -312,27 +313,27 @@ void thread::spin_wait(int32 iterations) {
312313
}
313314

314315
void thread::start() {
315-
if (!is_unstarted()) throw thread_state_exception {};
316+
if (!is_unstarted()) throw_helper::throws(exception_case::thread_state);
316317
if (data_->thread_start.is_empty() && data_->parameterized_thread_start.is_empty()) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation);
317318
data_->state &= ~xtd::threading::thread_state::unstarted;
318319
data_->joinable = true;
319320
data_->parameter = nullptr;
320321
data_->handle = native::thread::create([&](intptr arg) {
321322
reinterpret_cast<threading::thread*>(arg)->thread_proc();
322323
}, reinterpret_cast<intptr>(data_->safe_thread), data_->max_stack_size, is_suspended(), data_->thread_id);
323-
if (data_->handle == invalid_handle) throw io::io_exception {};
324+
if (data_->handle == invalid_handle) throw_helper::throws(exception_case::io);
324325
}
325326

326327
void thread::start(std::any param) {
327-
if (!is_unstarted()) throw thread_state_exception {};
328+
if (!is_unstarted()) throw_helper::throws(exception_case::thread_state);
328329
if (data_->parameterized_thread_start.is_empty()) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation);
329330
data_->state &= ~xtd::threading::thread_state::unstarted;
330331
data_->joinable = true;
331332
data_->parameter = param;
332333
data_->handle = native::thread::create([](intptr arg) {
333334
reinterpret_cast<thread*>(arg)->thread_proc();
334335
}, reinterpret_cast<intptr>(data_->safe_thread), data_->max_stack_size, is_suspended(), data_->thread_id);
335-
if (data_->handle == invalid_handle) throw io::io_exception {};
336+
if (data_->handle == invalid_handle) throw_helper::throws(exception_case::io);
336337
}
337338

338339
thread thread::start_new(const xtd::threading::thread_start& start) {
@@ -348,7 +349,7 @@ thread thread::start_new(const xtd::threading::parameterized_thread_start& start
348349
}
349350

350351
void thread::suspend() {
351-
if (!is_alive()) throw thread_state_exception {};
352+
if (!is_alive()) throw_helper::throws(exception_case::thread_state);
352353

353354
data_->state |= xtd::threading::thread_state::suspend_requested;
354355
native::thread::suspend(data_->handle);
@@ -485,7 +486,7 @@ void thread::interrupt_internal() {
485486
data_->interrupted = false;
486487
data_->state &= ~xtd::threading::thread_state::wait_sleep_join;
487488
if (data_->managed_thread_id == 1) environment::raise(signal::interrupt);
488-
else throw thread_interrupted_exception {};
489+
else throw_helper::throws(exception_case::thread_interrupted);
489490
}
490491

491492
bool thread::is_aborted() const noexcept {
@@ -546,8 +547,8 @@ void thread::thread_proc() {
546547
else if (!data_->parameterized_thread_start.is_empty()) data_->parameterized_thread_start(data_->parameter);
547548
else xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation);
548549

549-
if (is_aborted()) throw thread_abort_exception {};
550-
if (is_aborted()) throw thread_interrupted_exception {};
550+
if (is_aborted()) throw_helper::throws(exception_case::thread_abort);
551+
if (is_aborted()) throw_helper::throws(exception_case::thread_interrupted);
551552

552553
data_->state |= xtd::threading::thread_state::stopped;
553554
data_->end_thread_event.set();

src/xtd.core/src/xtd/timers/timer.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "../../../include/xtd/math.hpp"
77

88
using namespace xtd;
9+
using namespace xtd::helpers;
910
using namespace xtd::threading;
1011
using namespace xtd::timers;
1112

@@ -68,7 +69,7 @@ bool timer::enabled() const noexcept {
6869
}
6970

7071
timer& timer::enabled(bool value) {
71-
if (data_->closed) throw object_closed_exception {};
72+
if (data_->closed) throw_helper::throws(exception_case::object_closed);
7273
if (data_->enabled && !value) data_->sleep.set();
7374
data_->enabled = value;
7475
if (value) thread_pool::queue_user_work_item(data_->timer_proc, this);
@@ -80,7 +81,7 @@ double timer::interval() const noexcept {
8081
}
8182

8283
timer& timer::interval(double value) {
83-
if (data_->closed) throw object_closed_exception {};
84+
if (data_->closed) throw_helper::throws(exception_case::object_closed);
8485
if (math::ceiling(value) < 0 || math::ceiling(value) > int32_object::max_value) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::argument);
8586
data_->interval = time_span::from_milliseconds(value);
8687
return *this;
@@ -101,7 +102,7 @@ timer& timer::synchronizing_object(std::nullptr_t value) {
101102
}
102103

103104
void timer::close() {
104-
if (data_->closed) throw object_closed_exception {};
105+
if (data_->closed) throw_helper::throws(exception_case::object_closed);
105106
stop();
106107
data_->event.wait_one();
107108
data_->closed = true;

0 commit comments

Comments
 (0)