Challenge: Exception Translation
In this challenge, you are asked to implement a function that demonstrates exception translation. Your goal is to catch a standard exception, such as std::out_of_range, and then throw a custom exception instead. This technique is useful for abstraction, allowing you to hide implementation details and provide a consistent interface for error handling in your code.
Swipe to start coding
Write a function named translateException that takes a std::vector<int>& and an int index as arguments. The function should attempt to access the element at the given index. If an out-of-range access occurs, catch the std::out_of_range exception and throw a custom exception class named IndexError instead. The IndexError class should inherit from std::exception and override the what() method to return the message "Index out of range".
Løsning
solution.cpp
Takk for tilbakemeldingene dine!
single
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Awesome!
Completion rate improved to 6.67
Challenge: Exception Translation
Sveip for å vise menyen
In this challenge, you are asked to implement a function that demonstrates exception translation. Your goal is to catch a standard exception, such as std::out_of_range, and then throw a custom exception instead. This technique is useful for abstraction, allowing you to hide implementation details and provide a consistent interface for error handling in your code.
Swipe to start coding
Write a function named translateException that takes a std::vector<int>& and an int index as arguments. The function should attempt to access the element at the given index. If an out-of-range access occurs, catch the std::out_of_range exception and throw a custom exception class named IndexError instead. The IndexError class should inherit from std::exception and override the what() method to return the message "Index out of range".
Løsning
solution.cpp
Takk for tilbakemeldingene dine!
single