rethrow.java
来自「程序练习中包括书中实例程序代码和练习中要用到的代码,是压缩文件」· Java 代码 · 共 48 行
JAVA
48 行
package examples.exceptions;
/** A class to demonstrate rethrowing an exception
*/
public class ReThrow {
/** A utility class method that always throws
* an exception
* @exception IllegalValueException For
* demonstration purposes
*/
public static void methodThrowsException()
throws IllegalValueException {
throw new IllegalValueException( 150 );
}
/** A utility class method that catches, alters,
* and rethrows IllegalValueException exceptions
* @exception IllegalValueException An uncaught
* exception from a called method
*/
public static void methodReThrowsException()
throws IllegalValueException {
try {
methodThrowsException();
}
catch( IllegalValueException ive ) {
System.out.println( "Caught illegal value "
+ ive.getValue() );
if ( ive.getValue() > 100 ) {
ive.setValue( ive.getValue() - 100 );
throw ive;
}
}
}
/** Test method for the class
* @param args Not used
*/
public static void main( String[] args ) {
try {
methodReThrowsException();
}
catch( IllegalValueException ive ) {
System.out.println( "Caught illegal value "
+ ive.getValue() );
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?