?? finally.java
字號:
//finally.java
public class finally
{
public static void main(String args[])
{
try {
throwException();
}
// catch Exceptions thrown by method throwException
catch ( Exception exception )
{
System.err.println( "Exception handled in main" );
}
doesNotThrowException();
}
// demonstrate try/catch/finally
public static void throwException() throws Exception
{
// throw an exception and immediately catch it
try {
System.out.println( "Method throwException" );
throw new Exception(); // generate exception
}
catch ( Exception exception )
{
System.err.println("Exception handled in method throwException" );
throw exception; // rethrow for further processing
// any code here would not be reached
}
// this block executes regardless of what occurs in
// try/catch
finally {
System.err.println("Finally executed in throwException" );
}
// any code here would not be reached
}
// demonstrate finally when no exception occurs
public static void doesNotThrowException()
{
// try block does not throw an exception
try {
System.out.println( "Method doesNotThrowException" );
}
// catch does not execute, because no exception thrown
catch( Exception exception )
{
System.err.println( exception.toString() );
}
finally {
System.err.println("Finally executed in doesNotThrowException" );
}
System.out.println("End of method doesNotThrowException" );
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -