Reputation: 1
i am trying to use a custom exception on my solution where when user input other than "iammanager" there shall be an error message. However, when i try to run the code there is an error said that 'exception is never thrown in a body of a corresponding try statement". Here is my code for the custom exception class.
public class InvalidCodeException extends Exception{
//InvalidCodeException(String message){
// super(message);
//}
String managerN ="iammanager";
public InvalidCodeException(String conditionViolated)
{
super("Invalid Password: ");
managerN = conditionViolated;
}
public String printMessage()
{
// Call constructor of parent Exception
// according to the condition violated
switch (managerN) {
// Password should not contain any space
case "one":
return ("Password should not contain any space");
// Password should contain at least
// one special character ( @, #, %, &, !, $ )
case "two":
return ("Password should not contain any special characters");
// Password should not contain any numbers
case "three":
return ("Password should not contain any number");
// Password should contain at least
// one special character ( @, #, %, &, !, $ )
// case "four":
// return ("You do not have access!");
}
return ("You do not have access!");
}
}
and this is my code on the jform class where when user input the name on the jtextfield and the button was pressed it should display an error message if the user input other than "iammanager"
public static void isValid(String managerN)
throws InvalidCodeException
{
// to check space
if (password.contains(" ")) {
throw new InvalidCodeException("one");
}
// for special characters
if (!(managerN.contains("@") || managerN.contains("#")
|| managerN.contains("!") || managerN.contains("~")
|| managerN.contains("$") || managerN.contains("%")
|| managerN.contains("^") || managerN.contains("&")
|| managerN.contains("*") || managerN.contains("(")
|| managerN.contains(")") || managerN.contains("-")
|| managerN.contains("+") || managerN.contains("/")
|| managerN.contains(":") || managerN.contains(".")
|| managerN.contains(", ") || managerN.contains("<")
|| managerN.contains(">") || managerN.contains("?")
|| managerN.contains("|"))) {
throw new InvalidCodeException("two");
}
// for numbers
if (!(managerN.contains("0") || managerN.contains("1")
|| managerN.contains("2") || managerN.contains("3")
|| managerN.contains("4") || managerN.contains("5")
|| managerN.contains("6") || managerN.contains("7")
|| managerN.contains("8") || managerN.contains("9"))) {
throw new InvalidCodeException("three");
}}
private void confirmActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String managerN = "iammanager";
try {
managerN = managerName.getText();
JOptionPane.showMessageDialog(null, "Welcome to User List Manager!" );
}
catch(InvalidCodeException e) {
JOptionPane.showMessageDialog(null, e.getMessage() );
JOptionPane.showMessageDialog(null, e.printMessage() );
}
}
Updated try catch statement:
private void confirmActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String managerN = "iammanager" ;
try {
isValid(managerN);
managerName.getText().equals("iammanager");
JOptionPane.showMessageDialog(this, "Welcome to User List Manager !");
//calling user list page
UserList userP = new UserList();
userP.show();
dispose();
}
catch(InvalidCodeException e) {
JOptionPane.showMessageDialog(this, "You do not have access");
}
}
when i use this, the error message for try catch disappeared. however the output was not something that i want, the output for this code was it still printed yo do not have access even though i put the correct value which is "iammanager". can anyone tell me which part did i do wrong?
Upvotes: 0
Views: 140