有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

swing如何删除java消息的一部分。lang.IllegalArgumentException处理异常

我正在开发一个Swing应用程序,它有一个表单,我在这个表单上使用异常处理多个验证。由于知道Java 7可以使用mutli catch语句,我用它来改进我的代码,但从那以后Stacktrace总是从以下内容开始:

“java.lang.Exeption:”+处理异常消息

如何删除此消息的“java.lang.Exeption:”

下面是我的一些验证和执行的示例

动作事件:

  public void actionPerformed(ActionEvent arg0){

       //Validation 1
       NomeGrupoVazio(string1);

       //Validation 2
       ValidaString(string2);

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


   }

有效期:

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");
    }

提前谢谢


共 (1) 个答案

  1. # 1 楼答案

    我猜,您正在使用Exception#toString()获取异常消息。尝试使用Exception#getMessage()

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

    希望有帮助