Que métodos geram exceção?
A documentação da API Java indica, para cada método, se ele pode gerar
ou não uma ou mais exceções. Por exemplo, na documentação do método
java.lang.Integer.parseInt(String) lê-se:
parseInt
public static int parseInt(String s)
throws NumberFormatException
- Parses the string argument as a signed decimal
integer. The characters in the string must all be
decimal digits, except that the first character may be
an ASCII minus sign
'-' to indicate a
negative value.
- Parameters:
- s - a string.
- Returns:
- the integer represented by the argument in decimal.
- Throws: NumberFormatException
- if the string does not contain a parsable integer.
|
Nesse método, se o argumento (uma string) não representar adequadamente um
valor inteiro decimal (ou seja, composto exclusivamente pelos caracteres
dígitos de 0 a 9, opcionalmente com um sinal '-' à frente), não será
possível gerar um valor inteiro correspondente e uma exceção de nome
NumberFormatException será gerada.
Outro exemplo: a documentação da classe java.io.InputStream
indica que o método read() também pode gerar uma exceção:
read
public int read(byte b[]) throws IOException
- Reads up to
b.length bytes of data from this
input stream into an array of bytes.
The read method of InputStream
calls the read method of three arguments
with the arguments b , 0 , and
b.length .
- Parameters:
- b - the buffer into which the data is read.
- Returns:
- the total number of bytes read into the buffer, or
-1 if there is no more data because the
end of the stream has been reached.
- Throws: IOException
- if an I/O error occurs.
|
© Ivan Luiz Marques Ricarte
DCA/FEEC/UNICAMP
Last modified: Mon Nov 8 12:25:59 EDT 1999