<<< Throwing and re-throwing an exception, cont. | Index | catching IOException example >>> |
The syntax for the declaration of a method that throws exceptions:
modifiers returnType methodName( [parameterList] )
throws exceptionList {}
A method that throws an IOException:
public static long getFileLength() throws IOException
{
RandomAccessFile in =
new RandomAccessFile( path, "r" );
long length = in.length(); // may throw IOException
return length;
}
A method that throws an Exception
public static void myMethod() throws Exception
{
throw new Exception( "Exception generated in myMethod" );
}
}
<<< Throwing and re-throwing an exception, cont. | Index | catching IOException example >>> |