Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Java Exception for an invalid login to MySQL server? 1

Status
Not open for further replies.

peterv12

Technical User
Dec 31, 2008
108
0
0
US
I'm new to java, so please be kind. I've written a little java script that displays a login screen, with username and password. It works fine, except when an invalid username or password is entered. It just blows up (as expected). What I'd like to know is if there is some way to code a "catch" that will prevent the blow-up and just redisplay the login screen. I've been searching, but haven't found anything yet. Thanks in advance.
Peter V.
 
Thanks for the link, but I have looked at that previously. The question I have is where can I find out how to specify the exact exception I need? The example shows "ArrayIndexOutOfBoundsException". Is there somewhere I can find a list of all the exceptions? I'm looking for an exception of an invalid login to MySQL. I have no idea what that would be called. If anyone knows, please help!
Thanks,
Peter V.
 
Try catching a generic Exception and printing a trace:
Code:
try
{
...
}catch(Exception e)
{
   e.printStackTrace();
}

This will print in stdout something like:
Code:
      java.lang.NullPointerException
         at MyClass.mash(MyClass.java:9)
         at MyClass.crunch(MyClass.java:6)
         at MyClass.main(MyClass.java:3)
And this should show the exact exception you need to catch.
 
Never do that! It's a bad practice to catch generic exceptions because you will catching whatever errors can come.

There are several kinds of exceptions, those that should be caught and that you should know you need to catch like a SQLException, because the database may be down and you cannot control it; and those that reflect programming error, like NullPointer or the ArrayIndex you have.

Maybe if you post the relevant piece of code we can have a look and be more precise

Cheers,
Dian
 
That's why I've told him to do a trial, see the exception, and then he will be able to catch the exact exception in his code.

But as Diancecht said, it's a bad idea to let a generic exception in the final code.

 
You don't need to do a generic catch to see the exception, you will see the whole stack trace when it's generated

Cheers,
Dian
 
Thanks guys! I've now determined it's an SQLException, and have coded a try/catch to handle it. Your help is greatly appreciated!
Peter V.
 
And what's gonna happen with the next exception? Try/catch sentences are a part of the program and need to be known beforehand by means of throws declaration.

Catching exceptions when arising is not a good practice.

Cheers,
Dian
 
Wouldnt it be an instance of SQLClientInfoException thrown in this particular example?

Failing that, you could always capture SQLException, thus avoiding pitfalls of catching the general Exception case (not so good)

I'd say put it in your throws clause though - make sure your finally closes your Connections etc too. :)
 
Diancecht, As for "throwing" exceptions, I do not know how to do that yet. I'm new to java and am trying to learn it via "book" learning. Now that you've pointed this out, I'll look into it. Thanks for the advice.

chris921, I'mve made sure the connections have been closed, but I'll have to look into the "finally" part. I appreciate the information.
Peter V.
 
Well, Peter, take your time, exception handling is an important part of Java, it's worth having a detailed look at it.

Just a hint, in the method signature there's sometimes a throws clause that says: "Hey, I may throw this exception" and that will mean you need to catch it.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top