Results 1 to 7 of 7
  1. #1
    Loop is offline Member
    Join Date
    Sep 2008
    Posts
    36
    Rep Power
    0

    Smile 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;)

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    once you get the user input, need to validate. And after that convert it into an int, inside a try-catch block.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    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>)>;

  4. #4
    Loop is offline Member
    Join Date
    Sep 2008
    Posts
    36
    Rep Power
    0

    Default

    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?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    I do not want it to exit
    Don't your try{}catch{} clauses catch the exception and allow you to handle the case in your code?
    What do you want to happen when you catch an exception?
    I want it to display the message and then return the user to the "menu".
    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.

  6. #6
    Loop is offline Member
    Join Date
    Sep 2008
    Posts
    36
    Rep Power
    0

    Default

    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.



    }
    }

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    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

  1. Putting code together.
    By newbee in forum New To Java
    Replies: 3
    Last Post: 04-17-2008, 03:53 AM
  2. Putting your own type in a Set
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 07:32 PM
  3. How to use chained exceptions
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-04-2008, 02:50 PM
  4. Putting controls in arrays?
    By Cymro in forum New To Java
    Replies: 4
    Last Post: 02-01-2008, 06:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •