1

I'm developping a Swing application, with a form on which I process several validations using exceptions. Knowing that since Java 7 it is possible to use a mutli-catch statement, I used it to improve my code, but since then the Stacktrace always begins with :

"java.lang.Exeption :" + Handling Exception Message.

How I could remove "java.lang.Exeption :" of this message ?

Here is an example from some of my validations and execution.

ActionEvent:

  public void actionPerformed(ActionEvent arg0){

       //Validation 1
       NomeGrupoVazio(string1);

       //Validation 2
       ValidaString(string2);

       //Validation 3
       DiferenciarRMs(int1, int2, int3, int4);


   }

Valitadions:

private void NomeGrupoVazio(String nome) {
            if (nome.isEmpty()) {
                   throw new IllegalArgumentException("Preencha o nome do grupo!");
    }
}

private void ValidaString(String s) {

    Pattern pattern = Pattern.compile("[0-9]");
    Matcher matcher = pattern.matcher(s);
    if (matcher.find()) {
        throw new IllegalArgumentException(
                "Por favor digite apenas caracteres não numéricos nos campos referentes aos nomes");
    }
}

private void DiferenciarRMs(int rm1, int rm2, int rm3, int rm4) {
    if (rm4 == 0) {
        if (rm1 == rm2 || rm1 == rm2 || rm1 == rm3 || rm2 == rm3) {
            throw new IllegalArgumentException(
                    "Todos os RM's precisam ser diferentes");

        }
    } else if (rm1 == rm2 || rm1 == rm3 || rm1 == rm4 || rm2 == rm3
            || rm2 == rm4 || rm3 == rm4) {
        throw new IllegalArgumentException(
                "Todos os RM's precisam ser diferentes");
    }

Thanks in advance!

Dici
  • 25,226
  • 7
  • 41
  • 82
  • I dont see where you are handling exceptions, so far you are just throwing some. Please show the code where you hadle them and where the message is created. – A4L Oct 16 '14 at 11:18
  • 1
    part of the JComponent has implemented methods for validation in API, code example posted here talking about nothing (without an SSCCE/MVCE) – mKorbel Oct 16 '14 at 11:27
  • Why do you want to remove it? It is useful information if the exception indicates a bug. It is [questionable whether you should report the message text of exceptions for exceptions that do not indicate a bug](http://stackoverflow.com/questions/7320080/should-you-report-the-message-text-of-exceptions). – Raedwald Mar 24 '16 at 17:30

1 Answers1

4

I guess, you are using Exception#toString() to get the exception message. Try using Exception#getMessage() instead.

Exception ex = new IllegalArgumentException("My Error Message!");
System.out.println(ex.toString()); 
System.out.println(ex.getMessage());

Hope that helps.

DirkNM
  • 2,614
  • 15
  • 21
  • 1
    You may be right, but as you said it just a *guess*. I think you should have asked it in a comment – Dici Oct 16 '14 at 11:23