xtd 0.2.0
Loading...
Searching...
No Matches
argument_exception.cpp

Shows how to use argument_exception exception.

#include <xtd/argument_exception>
#include <xtd/console>
using namespace xtd;
int divide_by_two(int num) {
// If num is an odd number, throw an argument_exception.
if ((num & 1) == 1) throw argument_exception("Number must be even");
// num is even, return half of its value.
return num / 2;
}
auto main() -> int {
// argument_exception is not thrown because 10 is an even number.
console::write_line("10 divided by 2 is {0}", divide_by_two(10));
try {
// argument_exception is thrown because 7 is not an even number.
console::write_line("7 divided by 2 is {0}", divide_by_two(7));
} catch (const argument_exception&) {
// Show the user that 7 cannot be divided by 2.
console::write_line("7 is not divided by 2 integrally.");
}
}
// This code produces the following output :
//
// Exception occured :
// -------------------
// 10 divided by 2 is 5
// 7 is not divided by 2 integrally.
The exception that is thrown when one of the arguments provided to a method is not valid.
Definition argument_exception.h:24
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10