+ 2

C++ Challenge question 1

Rearrange the code to define a function "foo", which throws an exception with a value of "-100" if its parameter is greater than 999. Then "foo" catches its exceptions and prints "error!" to the screen. -void foo(int arg) { -if (arg > 999) throw -100; } -} -cout << "error!" << endl; } -try { -catch (int x) {

7th Apr 2017, 11:35 AM
sophie
13 Réponses
+ 7
then the question-creator has specified wrong answer @Sophie
7th Apr 2017, 1:10 PM
Meharban Singh
Meharban Singh - avatar
+ 6
the bracket for "if" is facing the wrong way. not this if }} do this if {} hope it helps.
7th Apr 2017, 2:28 PM
Manual
Manual - avatar
+ 5
1,5,2,3,6,4
7th Apr 2017, 11:45 AM
Meharban Singh
Meharban Singh - avatar
+ 4
-void foo(int arg) { -try { -if (arg > 999) throw -100; } -catch (int x) { -cout << "error!" << endl; } -}
22nd May 2017, 2:39 PM
Benmana Reda
+ 1
I Tried this but it says its worng? -void foo(int arg) { -try { -if (arg > 999) throw -100; } -} -catch (int x) { -cout << "error!" << endl; }
7th Apr 2017, 12:01 PM
sophie
+ 1
the "} " must be at the bottom. the if line already has a closing curly bracket
7th Apr 2017, 12:47 PM
Petja Boigk
Petja Boigk - avatar
+ 1
dunno why I got a thumbs down while I answer correctly the question . so a more verbose answer. @sophie it's an arrange the code question. look at the if-line. it doesn't open any curly brackets but closes one. if then the next line another curly bracket follows you close the function. this line only containing the curly bracket must therefore be the very last line....
7th Apr 2017, 2:33 PM
Petja Boigk
Petja Boigk - avatar
+ 1
try this void foo(int arg) { try { if (arg > 999) throw -100; } catch (int x) { cout << "error!" << endl; } }
7th May 2018, 9:58 AM
abdiwahab jama said
abdiwahab jama said - avatar
0
void foo(int arg) { try { if (arg > 999) throw -100; } catch (int x) { cout << "error!" << endl; } }
1st Jul 2017, 4:51 PM
KRISHNASAMI S
KRISHNASAMI S - avatar
0
they told ans wrong
8th Jun 2018, 11:27 AM
Gayathri
0
no result can help me to solve this problem
17th Mar 2019, 7:55 PM
Abbes Raouf
Abbes Raouf - avatar
0
answer is try, throw, and catch.
15th Jan 2020, 9:24 AM
Imanuel Wicaksono
Imanuel Wicaksono - avatar
0
-void foo(int arg) { -try { -if (arg > 999) throw -100; } -catch (int x) { -cout << "error!" << endl; } -}
6th Jun 2020, 9:03 PM
Frederick John Suerte
Frederick John Suerte - avatar