programming-examples/java/Core_Java/Using Exceptions.java

60 lines
1.6 KiB
Java
Raw Normal View History

2019-11-15 12:59:38 +01:00
Using Exceptions
public class UsingExceptions
{
public static void main( String args[] )
{
try
{
throwException();
}
catch ( Exception e )
{
System.err.println( "Exception handled in main" );
}
doesNotThrowException();
}
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 e )
{
System.err.println( "Exception handled in method throwException" );
throw e; // rethrow e for further processing
// any code here would not be reached
}
finally
{
System.err.println( "Finally executed in throwException" );
}
// any code here would not be reached
}
public static void doesNotThrowException()
{
try
{
System.out.println( "Method doesNotThrowException" );
}
catch( Exception e )
{
System.err.println( e.toString() );
}
finally
{
System.err.println( "Finally executed in doesNotThrowException" );
}
System.out.println( "End of method doesNotThrowException" );
}
}