Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.


+7 votes

We were thinking of using a try/catch block to make sure that the enemy car could be created using a URL. if the URL is invalid then we don't want the program to execute. However, I was reading about exceptions and it says that you cannot declare a variable in a try/catch because it's scope is only within the block. So when I tried to call the enemyCar constructor and pass in variables including the URL, it left the scope of the rest of the program. Where else could I put the try catch block to check for this error?

asked in CSC305 Fall 2020 by (1 point)

2 Answers

+3 votes

You could try to put the try catch block inside the constructor for the enemyCar. If you think the URL is the part that will cause problems try to put that part of the constructor inside your try block.

answered by (1 point)

I hadn't thought of putting the try in the constructor; it even makes more sense to. I'll give that a try and see what happens

+2 votes

You could create default variables and there is nothing there so it doesn't have that error.

answered by (1 point)