![]() |
Try and Catch Statements
Ok, I have this code and I am trying to put try and catch statements in it and I am unable to. I receive a compiler error that says can't have try without catch, and catch without try.
How can I do this? :
import java.io.*; |
Re: Try and Catch Statements
The reason behind this is bracket placement, what you have actually done in yurs is place the catch statement within the try statement
:
Note i have also removed the else statement. Now you will be given a different error converning the fact that TheException is never thrown in the try statement, this i'll let you look into. If you do still need help on this just ask, but as a clue what exception are you trying to catch? and what throws it? Cheers, Chris |
Re: Try and Catch Statements
Quote:
I never got to see the error because I figured out rather quickly with you clue. :) Thanks, I wasn't throwing an exception, I never had the statement. I put it in their and now I am receiving the illegal input error correctly. Now I just have to modify my questions and call the exception for every question. Thanks again. This was the code after I modified it with your help. :
import java.io.*; |
Re: Try and Catch Statements
Indeed glad you got it.
Chris |
Re: Try and Catch Statements
Ok, I have modified it more, and now I am going crazy because I can't see what I am doing wrong. I am sure that it is a bracket or something somewhere, but Ihave been here for 3 hours and I am not getting anywhere. This is what it is suppose to do.
ask if you like cookies if yes --> do you like chocolate if yes --> you must like oreo cookies then. if no --> you must like peanut butter. end this section and continue with do you like milk, otherwise if you don't like cookies ---> do you like milk if yes ---> do you like chocolate milk if yes --> me and you both pal. if no --> I don't understand you, goodbye. it compiles, but when I get to the do you like milk section, it doesn't allow me to answer it, and says me and you too pal. when asks me if i like oreo cookies, if I say no it says ok, so you don't like milk. Any ideas? :
import java.io.*; |
Re: Try and Catch Statements
:
You had an if statement that was checking if the input was = to "n", and if it was then it was printing "Do you like milk?", otherwise it was skipping this line. :
Also once you had asked whether or not they like milk, you then printed do you like chocolate milk and forgot to ask the user again, and so the last input is used as a check. Cheers, Chris |
| All times are GMT -5. The time now is 1:14 PM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC