public class Main /*时 代 J a v a 提供*/
{
public static void main(String[] args)
{
try
{
throwException();
}
catch (Exception exception) // exception thrown by throwException
{
System.err.println("Exception handled in main");
}
doesNotThrowException();
}
/*
from n o w j a v a . c o m - 时代Java
*/
// demonstrate try...catch...finally
public static void throwException() throws Exception
{
try // throw an exception and immediately catch it
{
System.out.println("Method throwException");
throw new Exception(); // generate exception
}
catch (Exception exception) // catch exception thrown in try
{
System.err.println(
"Exception handled in method throwException");
throw exception; // rethrow for further processing
// code here would not be reached; would cause compilation errors
}
finally // executes regardless of what occurs in try...catch
{
System.err.println("Finally executed in throwException");
}
// code here would not be reached; would cause compilation errors
}
// demonstrate finally when no exception occurs
public static void doesNotThrowException()
{
try // try block does not throw an exception
{
System.out.println("Method doesNotThrowException");
}
catch (Exception exception) // does not execute
{
/**代码未完, 请加载全部代码(NowJava.com).**/
本文系作者在时代Java发表,未经许可,不得转载。如有侵权,请联系nowjava@qq.com删除。