Results 1 to 7 of 7
- 10-13-2008, 05:58 PM #1
Member
- Join Date
- Sep 2008
- Posts
- 36
- Rep Power
- 0
Need help putting Exceptions in my program.
Hey, everyone, time to use my reason for joining the forum.
A few weeks ago we got a project to do. My progress has been swift and my program is complete, I just need to put in some Exceptions to help control the problem. Now I'm not gonna post my entire piece of code, that would take too long I'm just gonna give you a taste.
import javax.swing.*;
public class Main//
{
public static void main(String[]args)
{
Add ad = new Add();// Creates an object of the add class to some addition.
try
{
do
{
String Menu = JOptionPane.showInputDialog("Choose a submenu by entering a number");
int menu = Integer.parseInt(Menu);
//A lot of code
}
while(menu != 5)'
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, "An Error Occurred");
}
}
}
The code above is not important, it's just to give you a taste of my program.
See, how I made the try and catch? While going through my program and someone inputs a blank Enter or a string when they're supposed to input a number, the program delivers the message, but does not return me to the menu or to the, for instance, addition sum, they were busy with.
Please help, but keep in mind the code above is just a small sample of the real code, to give you an idea of my main class. The try and catch is all that is important.
Loop;)
- 10-13-2008, 06:08 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
once you get the user input, need to validate. And after that convert it into an int, inside a try-catch block.
- 10-13-2008, 06:53 PM #3
What do you want to happen when you catch an exception?
You currently put out a message
Do you want to create and throw your own exceptions?
Create a new class for your exception that extends Exception.
then throw it where you want:
throw new <YourException(<Your args>)>;
- 10-13-2008, 07:23 PM #4
Member
- Join Date
- Sep 2008
- Posts
- 36
- Rep Power
- 0
I appreciate the feedback so far.
Okay, I may have made a weak explanation of what I want to do.
Say, I have a "Menu" in the do block
for instance
1. Add
2. Subtract
3. Multiply
4. Divide
The user puts in a value that represents a option, for instance, he/she types in 1 because he wants to do addition sums.
The problem is when he for instance, types in "ajdka" the program will end and the error message will show, but I do not want it to exit, I
want it to display the message and then return the user to the "menu", so that he/she has the opportunity to try again.
How would I go about doing that, using the 1st post code?
@Eranga
Hmmm, so, for instance I'm supposed to take the variable "Menu" out of the try but leave the int variable menu inside the try?
- 10-13-2008, 09:08 PM #5
Don't your try{}catch{} clauses catch the exception and allow you to handle the case in your code?I do not want it to exit
I want it to display the message and then return the user to the "menu".What do you want to happen when you catch an exception?
Your code shows a message. Now exit the method.
You'll have to show more of your code for us to be able to suggest how to continue.
- 10-13-2008, 09:41 PM #6
Member
- Join Date
- Sep 2008
- Posts
- 36
- Rep Power
- 0
Hmm, perhaps I should go read more about Exceptions...
None the less, I can give you more code, but it's on my other pc.
//Here's some code once menu receives a value.
if(Menu == 1)
{
String questionAmount = JOptionPane.showInputDialog("Input the number of questions");
int iqa = Integer.parseInt(questionAmount);
for(int x = 0; x <= iqa; x++)//using for, generate the amount of questions.
{
ad.getValue1();
ad.getValue2();
String answer = JOptionPane.showMessageDialog("What is "+ ad.getValue1() + " + " + ad.getValue2());
int answer2 = Integer.parseInt(answer);
//Now all this is in a try like in the first post.
}
}
- 10-13-2008, 11:48 PM #7
Not sure why you posted that code. Can you describe the program logic you are trying to implement?
If some code can create an exception (there are many causes of exceptions) and you want your program to be able to catch and handle the exception, you code try{}catch{} around the code and do something in the catch block. Often you put out an error message and exit the method. Here it gets more complicated. You might want to tell the caller of the method that an error occured. One way is to have the method return a special value for errors. Another way is to throw an exception. Then the caller of the method will need its own try{}catch{} block to handle that exception and so on.
Similar Threads
-
Putting code together.
By newbee in forum New To JavaReplies: 3Last Post: 04-17-2008, 03:53 AM -
Putting your own type in a Set
By Java Tip in forum java.langReplies: 0Last Post: 04-15-2008, 07:32 PM -
How to use chained exceptions
By Java Tip in forum java.langReplies: 0Last Post: 04-04-2008, 02:50 PM -
Putting controls in arrays?
By Cymro in forum New To JavaReplies: 4Last Post: 02-01-2008, 06:54 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks