Hata iletileri
C++ fonksiyonların, bir istisna fırlatmak için stop() fonksiyonunu çağırabilir. Senin için kısmen tanımlanmış bir add_positive_numbers() fonksiyonu verdik. Bu fonksiyonu, istisnalar fırlatacak şekilde tamamla.
Bu egzersiz, kursun bir parçasıdır
Rcpp ile R Kodunu Optimize Etme
Egzersiz talimatları
- Eğer
xnegatifse, şu iletisiyle bir istisna fırlat: "x is negative". - Eğer
ynegatifse, şu iletisiyle bir istisna fırlat: "y is negative".
Uygulamalı etkileşimli egzersiz
Bu egzersizi bu örnek kodu tamamlayarak deneyin.
cppFunction('
// adds x and y, but only if they are positive
int add_positive_numbers(int x, int y) {
// if x is negative, stop
if(___) stop("x is negative") ;
// if y is negative, stop
if(___) ___("y is negative") ;
return x + y ;
}
')
# Call the function with positive numbers
add_positive_numbers(2, 3)
# Call the function with a negative number
add_positive_numbers(-2, 3)