darkbluecast 20 de abr. de 2006
marciasp2611 20 de abr. de 2006
Sim, se não nem compilaria.
thingol 20 de abr. de 2006
Quando você passa null para Double.parseDouble, o resultado é uma NullPointerException, não uma NumberFormatException.
class TesteVazio {
/**
* Este programa imprime:
<pre>
Catch 1
Catch 2a
Catch 3
Catch 4
</pre>
(Testado em JDK 1.3, 1.4, 5.0, 6.0 da Sun, e efetuando as modificações necessárias,
em JDK 1.1 da Microsoft).
*/
public static void main ( String [] args ) {
String teste ;
teste = null ;
try {
Integer . parseInt ( teste );
} catch ( NumberFormatException ex ) {
System . out . println ( "Catch 1" );
} catch ( NullPointerException ex ) {
System . out . println ( "Catch 1a" );
}
try {
Double . parseDouble ( teste );
// Para JDK1.1 troque por: Double.valueOf (teste).doubleValue();
} catch ( NumberFormatException ex ) {
System . out . println ( "Catch 2" );
} catch ( NullPointerException ex ) {
System . out . println ( "Catch 2a" ); //
}
teste = "" ;
try {
Integer . parseInt ( teste );
} catch ( NumberFormatException ex ) {
System . out . println ( "Catch 3" );
}
try {
Double . parseDouble ( teste );
// Para JDK1.1 troque por: Double.valueOf (teste).doubleValue();
} catch ( NumberFormatException ex ) {
System . out . println ( "Catch 4" );
}
}
}
marciasp2611 20 de abr. de 2006
Estou usando a jdk 5.0
Agora sim, na conversão do String para double deixei o catch do NumberFormatException e do NullPointerException e funcionou!
Obrigado!