The Artima Developer Community
Sponsored Link

Java Answers Forum
Exceptions. Please help.

5 replies on 1 page. Most recent reply: Mar 22, 2006 2:22 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
joey taha

Posts: 3
Nickname: coolio2006
Registered: Mar, 2006

Exceptions. Please help. Posted: Mar 21, 2006 11:01 PM
Reply to this message Reply
Advertisement
Hi, i'm new to java (which is why im posting it here ). I was just wondering if someone can write me a sample code for exception. Nothing fancy, just a few lines. Lets say ive got my main function and thats doing something. In my main , im asking a user to input their first name (of course, this is string). What i want to do is, if the user has entered in nothing or if they've entered in a number (as a person cannot have numbers in their name), then i want an exception just saying that "You have entered a number as your first name, please try again". Basically, i want my exceptions to be handled in another class completely and i would like them to be triggered when that happens. Could someone help me please? Thanks.
Edit/Delete Message


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Exceptions. Please help. Posted: Mar 22, 2006 12:02 AM
Reply to this message Reply
public String getName() throws Exception {
    String s = javax.swing.JOptionPanel.showInputDialog(null, "Type in something");
    if (s == null || s.length < 1)
        throw new Exception("this is an error message");
    else
        return s;
}


When you call this, you do it like follows.

private testMethod() {
    try {
        String name = getName();
    } catch (Exception e) {
        javax.swing.JOptionPanel.showMessageDialog(null, e.getMessage(), "Error", javax.swing.JOptionPanel.ERROR_MESSAGE);
    }
}


You can put the getName method in any class you want.
You may want to make it static, so you don't need to create an instance of the class containing the method.

Note: I didn't compile the code, there may be some errors in the code, but nothing you can't resolve.
This is just to give you an idea.

joey taha

Posts: 3
Nickname: coolio2006
Registered: Mar, 2006

Re: Exceptions. Please help. Posted: Mar 22, 2006 12:17 AM
Reply to this message Reply
Thanks for your reply. Ok so if i'm getting user input from my main function, how does that interact with your code. If the user has inputted (for e.g. 12344555666) as their name, obviously that is wrong. So how does my code interact with the other exception class which i'm creating. Also, do i need to add anything in my main?

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Exceptions. Please help. Posted: Mar 22, 2006 1:11 AM
Reply to this message Reply
Pass the value to the processing method.
This method should evaluate the value and throw an exception if the value is not valid.

Do it the same way I did in the getName method.

joey taha

Posts: 3
Nickname: coolio2006
Registered: Mar, 2006

Re: Exceptions. Please help. Posted: Mar 22, 2006 1:13 AM
Reply to this message Reply
So for 50 strings, i'd have to pass 50 strings in all?

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Exceptions. Please help. Posted: Mar 22, 2006 2:22 AM
Reply to this message Reply
Pass them as array or in a loop, whatever you want to do.

Flat View: This topic has 5 replies on 1 page
Topic: ISO-8859-1 Previous Topic   Next Topic Topic: illegal start of type error

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use