<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">/*
    (c) Burton Rosenberg 1998. All rights reserved.

    A fifth example of Exceptions.
    
*/

// generally, you subclass Exception to make your  own,
// more specific exception

class LetterEException extends LetterException 
{
    LetterEException( String message )
    {
        super(message) ;
    }
}

class LetterAException extends LetterException 
{
    LetterAException( String message )
    {
        super(message) ;
    }
}

class LetterException extends Exception 
{
    LetterException( String message )
    {
        super(message) ;
    }
}

class ExEx5
{
    static int noEzzs( String s )
    throws LetterAException, LetterEException
    {
        int j = 0 ;
        for ( int i=0; i&lt;s.length(); i++ )
        {
            if (s.charAt(i)=='e')
            {
                LetterEException lee = new LetterEException("failed on " + s) ;
                throw lee ;
            }
            if (s.charAt(i)=='a')
            {
                LetterAException lae = new LetterAException("failed on " + s) ;
                throw lae ;
            }
            j++ ;
        }
        return j ;
    }
    
    static int arrayOfNoEzzs( String [] ss )
    throws LetterAException 
    {
        int j = 0 ;
        try {
            for ( int i=0; i&lt;ss.length; i++ )
            {
                j += noEzzs( ss[i] ) ;
            }    
        }
        catch ( LetterEException lee )
        {
            // do nothing with this exception, but 
            // the compiler will consider it handled.
        }
        return j ;
    }
    
    public static void main( String [] args )
    {
        String [] test1 = { "abc", "dfg", "hij" } ;
        String [] test2 = { "xbc", "def" } ;
        String [] test3 = { "xbc", "dfg", "mmm" } ;
        
        System.out.print("Trying arrayOfNoEzzs(test1)...") ;
        try {
            System.out.println( ExEx5.arrayOfNoEzzs(test1)) ;
            System.out.println( ExEx5.arrayOfNoEzzs(test2)) ;
            System.out.println( ExEx5.arrayOfNoEzzs(test3)) ;
        }
        catch ( LetterAException ex )
        {
            System.out.println("Exception thrown! " + ex) ;
        }
        System.out.println("Good-bye") ;
    }
    
}
</pre></body></html>