2015-04-29 28 views
2

我正在使用Fortify代碼分析工具。在我的代碼,我從XML獲取字符串並解析它加倍如何處理將字符串轉換爲雙倍的異常

a= Double.parseDouble(b); 

工具給我

The program calls a method that parses doubles and can cause the thread to hang. 

我怎樣才能提高我的代碼?

回答

1

您需要抓住可能的例外並處理它們,以便您的代碼不會翻倒。 有時B的值不會解析爲雙精度值。

try { 
    a = Double.parseDouble(b); 
} catch (NumberFormatException e) { 
    //the parseDouble failed and you need to handle it here 
} 

編輯:在「掛」的問題可以在這裏左右但是這是不可能的,但值得記住閱讀。 http://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/

1

這是一個JVM錯誤,爲了避免使用下面的代碼片段來解析到Double

BigDecimal d = BigDecimal(a); // throws NumberFormatException if it is string. 
Double b = d.doubleValue(); 
1

根據this注意,它只適用於Java版本6更新23和更早版本。如果您使用的是Java 6 update 24及更高版本,則相信您可以忽略它。

相關問題