How do I use custom exception in Java? I've followed the steps but am not getting my catch clauses to work

How do I use custom exception in Java? I've followed the steps but am not getting my catch clauses to work



I've been spending hours trying to figure out what's wrong with my java exception handling. I've followed all the books and sites in creating customs exceptions, throwing them, and attempting to catch them. They're extending RuntimeException (because they're thrown in an ActionEvent) so I don't think the throws clause needs to be declared in the method header. But the catch clauses won't run. Here's the pertinent code:


public void handle(ActionEvent event){
Object selectedButton = event.getSource();
if (selectedButton == b1)
{
String passwordStr1 = password1.getText();
String passwordStr2 = password2.getText();

if(passwordStr1.equals(passwordStr2))


if(PasswordCheckerUtility.isValidPassword(passwordStr1)== true)

Alert validAlert = new Alert(AlertType.INFORMATION);
validAlert.setTitle("Password Status");
validAlert.setHeaderText("Password is valid");
validAlert.showAndWait();



else
try

throw new UnmatchedException("The passwords do not match");

catch(UnmatchedException ue)
Alert unEAlert = new Alert(AlertType.ERROR);
unEAlert.setTitle("Password Status");
unEAlert.setContentText(ue.getMessage());





and


public class UnmatchedException extends RuntimeException

public UnmatchedException(String message)

super(message);








Are you missing unEAlert.showAndWait() in the catch block?

– SirRaffleBuffle
Sep 8 '18 at 3:02


unEAlert.showAndWait()






You say "the catch clauses won't run"; have you done any debugging to confirm whether or not this is true?

– Kevin Anderson
Sep 8 '18 at 3:22






@SirRaffleBuffle that's exactly right

– Jayson
Sep 8 '18 at 3:23






@Jayson, that's exactly right - how have you done any debugging? I don't see any debug code in your "else statement", so how do you even know that code is getting executed? Post your Minimal, Complete, and Verifiable example that demonstrates your problem. I include an "MCVE" in the edit to my answer. You code looks reasonable, but we don't know the context of how your code works. So simplify your code an post an "MCVE if you still have problems.

– camickr
Sep 8 '18 at 3:34



that's exactly right




2 Answers
2



I tried a more basic example and it seemed to work. Perhaps the issue is with some other aspect of your code. Here's what I used:



A main method:


public static void main(String...args)
handle(null);
//main()



The handle method:


public static void handle(ActionEvent event)
try
throw new UnmatchedException("The passwords do not match");
catch(UnmatchedException ue)
System.out.println(ue.getMessage());




And the custom exception in a separate class:


public class UnmatchedException extends RuntimeException
private static final long serialVersionUID = 1L;

public UnmatchedException(String message)

super(message);




Not really sure why you would throw/catch the Exception in the same method.



Exception processing is relatively expensive and should be avoided if possible. In your simple example you are just attempting to display an Alert message. That could easily be done in the "else statement".



I would typically only throw the Exception when you want the code that invoked the method to handle the Exception.



In any case throwing and catching the Exception in the same method works fine for me:


public class ExceptionTest

public static void main(String args)

validate("hello there");
validate("bye");


public static void validate(String text)

if (text.length() > 7)
System.out.println("everythings good");
else

try

if (text.length() < 8)
throw new StringTooShortException("Must be at least 8 characters");

catch (StringTooShortException e)

System.out.println(e);




static class StringTooShortException extends RuntimeException

public StringTooShortException(String message)

super(message);





When learning a new concept it is easier to start with simple code and get it working and then apply the knowledge to your real application. This problem is likely caused by your validation logic.






Exceptions in Java need to be caught OR declared, not both. Declaring the exception in the method header does nothing. If the exception is both caught and handled within the method, you do not need to declare it in the header.

– pseudobbs
Sep 8 '18 at 3:12






@pseudobbs, I did not suggest to handle the Exception in the method. I don't see the point of catching an Exception. Exception generate overhead. If you want to generate the Alert, then just do in in the else statement. If you really want to throw the Exception then I believe you need to declare in in the method header?

– camickr
Sep 8 '18 at 3:14







If the exception is caught in the same scope it is thrown, you do not need to declare it. If the exception is caught by a method calling the method that throws the exception, then you need to declare it. OP's question was about exception handling, not generating an Alert

– pseudobbs
Sep 8 '18 at 3:33



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế