Java Exception

zolf
zolf used Ask the Experts™
on
Hello there,

I am writing an application which connects to my db server from a android phone.the first step is to check the user credentials and then show the user the main page were the user has other options to choose from. now i dont have any exception handling in my code. someone told me that you need to create exception were you think it might go wrong. i added exception for empty username/password field and wrong credential.but the person told me that this is not correct. exception should not be used for these. can somebody please help me to explain were in my code i need to have exception and were not. i have created a Custom exception and have attached the code.please tell me if i am on the right path.

package custom.java.throwable;

public class CustomException extends Exception
{

	public CustomException(String detailMessage, Throwable throwable)
	{
		super(detailMessage, throwable);
		// TODO Auto-generated constructor stub
	}

	public CustomException(String detailMessage)
	{
		super(detailMessage);
		// TODO Auto-generated constructor stub
	}

	public CustomException(Throwable throwable)
	{
		super(throwable);
		// TODO Auto-generated constructor stub
	}
	

}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
The simplest answer - use exceptions wherever you feel that you will encounter exceptional cases . Exceptional cases are situations which  obstruct the normal flow of your business.
When an exceptional situation occurs , if you feel that your application still can continue after that , then it is worth catching that exception and handling it . Handling an exception depends on the particular case you encounter , say invalid user login !! You can catch that exception and put up a message to the user saying that it is an invalid login . An invalid login is a valid business case , and depending on the requirement you may suggest him a guest login .

But if your application encounters exceptional cases that are technical , say your code tries to access an index not present on an array , then it's purely a program error , and you SHOULD NOT handle these . These are called runtime exceptions and it is a very bad practice catching these exceptions

Summary - catch all those exceptional business cases and handle them appropriately .
Do not catch program(technical) errors

Author

Commented:
thanks for your feedback.
can you please tell me with reference to the CustomException i have shown above.how will the invalid or null string in username/passw field look like.please help

Author

Commented:
please help
@zolf , not sure what you are asking . But if is about how you should use your custom exception....

Wrap your login authentication code in try block  . When you find that the login fails , throw a corresponding CustomException and in the corresponding catch block ,handle it.

try
{
    //login code
    if(failedLogin)
      throw new CustomException("Invalid username/password");
}
catch(CustomException)
{
  //do whatever needs to be done incase of a failed login
}

Open in new window


Hope that helps

Author

Commented:
thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial