2015-03-31 58 views
2

正如標題所示,我試圖捕捉一個空字符串。我有一個類(我嘗試創建的對象的類)在String爲空或空時引發異常(使用str.isEmpty檢查)。 當我嘗試在另一個類中使用空字符串創建對象時,它按預期工作並引發異常。但是,我希望此類能夠捕獲該異常並通知用戶。但它似乎從來沒有捕捉異常,即使我嘗試寫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; 
} 

另一類(新飾品目的是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); 
         } 
+4

如果這是一個非法的參數,爲什麼不'IllegalArgumentException? – 2015-03-31 10:26:52

+1

您是否知道您不必拋出並捕獲異常來打印其堆棧跟蹤? 'new Exception(「message」)。printStackTrace();'也可以。 – 2015-03-31 10:27:47

+0

你在哪裏叫'新的有價值(名稱)'? – 2015-03-31 10:31:47

回答

-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 
} 
+0

捕捉'異常'是一個壞主意,除非你不幸有足夠的代碼拋出'Exception' - 你應該避免寫入,除非絕對必要,因爲它是返回Object的異常等價物(它基本上不提供類型信息)。如果需要特殊處理的新異常(例如'InterruptedException')被添加到方法簽名中,捕獲'異常'是一種很好的方式來隱藏問題。 – 2015-03-31 10:34:18

+0

我提到過我試過這個。它不起作用。另外,我想象用Exception捕捉各種異常很醜陋?因爲我認爲Exception是其他異常的超類,例如NumberFormatException? – user3130840 2015-03-31 10:35:21

3

首先拋出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; 
} 

不要捕獲異常。

其次,對其他類中捕獲一個RuntimeException,並顯示出mesage:

...}catch(RuntimeException exc) { 
         JOptionPane.showMessageDialog(exc.getMessage()); 
        } 

希望幫助你!

+0

是的!我現在看到,在它有機會在框架類* facepalm *中捕獲它之前,我在對象類中發現了異常。非常感謝! – user3130840 2015-03-31 10:47:42

2

已經提出了一個關於空字符串的問題,一個空字符串仍然不爲空,所以如果你想要捕獲它,它必須拋出「IllegalArgumentException」。