C++ syntax: Difference between revisions

Content deleted Content added
Tag: Reverted
Undid revision 1287318228 by 129.97.124.1 (talk) actually a bad idea, nvm
Line 482:
<syntaxhighlight lang="cpp" line="1">
import std;
 
using Exception = std::exception;
using OutOfRangeException = std::out_of_range;
using Vector = std::vector;
 
int main() {
try {
Vectorstd::vector<int> vec{3, 4, 3, 1};
int i{vec.at(4)}; // Throws an exception, std::out_of_range (indexing for vec is from 0-3 not 1-4)
} catch (const OutOfRangeExceptionstd::out_of_range& e) {
// An exception handler, catches std::out_of_range, which is thrown by vec.at(4)
std::println(stderr, "Accessing a non-existent element: {}", e.what());
} catch (const Exceptionstd::exception& e) {
// To catch any other standard library exceptions (they derive from std::exception)
std::println(stderr, "Exception thrown: {}", e.what());