Wouldn't it be sufficient just to surround the Console.WriteLine statement with the try-block? Why the array declaration as well? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Wouldn't it be sufficient just to surround the Console.WriteLine statement with the try-block? Why the array declaration as well?

21st Jul 2016, 8:00 PM
Martin Ed
Martin Ed - avatar
3 Answers
+ 2
Actually you don't need to surround the array declaration with the try-catch block. There's anything wrong with that line, it's just a normal array. What causes the problem is the Console.WriteLine() call, so it's the only one you need to surround, but it's a normal practice tu surround all of the related code in the same try-catch block. If you continue learning you will find the try-with-resources, wich is a nice example of why to surround more code. You can try to surround the method call only in the code playground, you will see it still works.
28th Oct 2016, 5:21 PM
Goio
+ 1
Hello! The reason we surrounded the array call was because there is nothing in the array at the index of 8. If we didn't handle it, it would raise an error that would end the program. I hope this cleared this up for you.
20th Sep 2016, 10:53 AM
Sean Mitchell
Sean Mitchell - avatar
0
Where is the code?
9th Aug 2016, 3:10 PM
James Flanders