捕获空字符串。使用哪个例外?

use*_*840 3 java swing exception

如标题所示,我试图捕获一个空字符串。我有一个类(我尝试创建的对象的类),当String为null或为空时(使用str.isEmpty检查),该类将引发Exception。当我尝试在另一个类中使用空字符串创建对象时,它会按预期工作并抛出异常。但是,我希望此类捕获该异常并通知用户。但是,即使我尝试编写Catch(Exception exc),它也似乎永远不会捕获异常。现在,我知道null或空字符串不是非法的。但是我的意图是对象类应该做到这一点。相反,似乎catch块根本不在乎。我开始认为我必须创建自己的某种异常类...或者我缺少什么?以下是代码的相关部分:

对象类的构造函数:

    public Valueables(String name){
    //name.trim().length() == 0
    if(name == null || name.isEmpty()){
        try {
            throw new Exception("Subclasses of Valueables cannot take in an empty String or null value for the \"name\" constructor");
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(2);
        }
    }
    else
        this.name = name;
}
Run Code Online (Sandbox Code Playgroud)

另一个类(新的Trinket对象是Valueables的子类。一个带有上面的构造函数代码的对象):

while(loopPassErrorControl == false){

                        //I don't think the try loop is relevant. But just to make sure...
                        //Otherwise just ignore it

                        try{
                            TrinketForm Tform = new TrinketForm();
                            answer = JOptionPane.showOptionDialog(ValueablesFrame.this, Tform, "Nytt smycke", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
                                    null, options , null);
                            if (answer != JOptionPane.OK_OPTION){
                            return;
                            }

                            valueablesList.add(new Trinket(Tform.getName(), Tform.getGemstones(), Tform.getMetalSelected()));
                            loopPassErrorControl = true;

                        }catch(NumberFormatException | NullPointerException exc) {
                        JOptionPane.showMessageDialog(ValueablesFrame.this, "Något gick fel");
                        }
                        }
                        //Test
                        for(Valueables obj : valueablesList){
                        System.out.println(valueablesList);
                        }
Run Code Online (Sandbox Code Playgroud)

Got*_*tal 6

已经提出了一个关于空字符串的问题,空字符串仍然不为空,所以如果你想捕获它,它必须抛出“IllegalArgumentException”。


小智 5

首先在Valuable上抛出RuntimeException:

public Valueables(String name){
    //name.trim().length() == 0
    if(name == null || name.isEmpty()){
        throw new RuntimeException("Subclasses of Valueables cannot take in an empty String or null value for the \"name\" constructor");

    }
    else
        this.name = name;
}
Run Code Online (Sandbox Code Playgroud)

并且不要捕获异常。

其次,在另一个类上捕获RuntimeException并显示消息:

...}catch(RuntimeException exc) {
                        JOptionPane.showMessageDialog(exc.getMessage());
                    }
Run Code Online (Sandbox Code Playgroud)

希望对您有所帮助!


Alv*_*ona -1

尝试将其捕获为通用异常,替换 NuumberFormatException 和 NullpointerException。

您也可以在 Java 中执行此操作。

try {
    //Some Code here
} catch (NumberFormatException | NullPointerException ex) {
    //Handle NumberFormat and NullPointer exceptions here    
} catch (Exception ex) {
    //Handle generic exception here
}
Run Code Online (Sandbox Code Playgroud)