[CPP] Try-Catch Clause
As java, we need a try-catch block in c++. The universal format of try-catch is
try{
program-statements
} catch (exception-declaration) {
handler-statements
} catch (exception-declaration) {
handler-statements
} //...
Standard exception
C++ standard library defines a set of classes which is used for exception. They are defined in the following 4 header files:- exception: defines the most general kind of exception class. It communicates only that an exception occurred but provides no additional information.
- stdexcept: defines several general-purpose exception classes.
- new: defines the bad_alloc exception type.
- type_info: defines the bad_cast exception type
stdexcept:
- exception: the most general kind of problem
- runtime_error: problems that can be detected only at run time
- range_error: runtime_error: result generated outside the range of values that are meaningful
- overflow_error: runtime_error: computation that overflowed
- underflow_error: runtime_error: computation that underflowed
- logic_error: Error in the logic of the program
- domain_error: logic error: argument for which no result exists
- out_of_range: logic error: used a value outside the valid range
what
The exception types define only a single operation named what. That function takes no arguments and returns a const char* that points to a C-style character string. The contents of this string depends on the type of exception object. For the types that takes a string initializer, the what function returns that string. For the other types, the value of the string that returns varies by compiler.Example: To read two integers from standard input and prints the result of dividing the first number by the second. And the 0 should be considered
Here, i use the try-catch as well as the type conversion(static cast)
评论
发表评论