
/**
 * Define a new checked exception.
 */
class MyCheckedException extends Exception
{
  public MyCheckedException( String msg )
  {
    super( msg );
  }
}

class Foo
{
  public static void getSomething( int x ) throws MyCheckedException
  {
      // for checked exception usually some condition that
      // may or may not be true depending on circumstances
    if( x == 3 )
      throw new MyCheckedException( "Something bad happened" );

    // Normal processing here
  }
}

class TestProgram
{
  public static void main( String[] args )
  {
    try
    {
      Foo.getSomething( args.length );

        // If no exception, flow continues
      System.out.println( "No exception occurred" );
    }
    catch( MyCheckedException e )
    {
      System.out.println( "Unexpected exception --> " + e );
      System.out.println( "Stack trace -->" );
      e.printStackTrace( );   //Can always dump a stack trace
    }
    finally
    {
        // This block is started whether or not exception is
        // never thrown, thrown but handled, or thrown but unhandled
        // This is the place to close files, graphics contexts,
        // sockets, and database connections.
      System.out.println( "Entered finally" );
    }
  }
}
