0

I have run into a problem while writing a parser. The following functions are all calling the GetSymbol() function. Also the functions call eachother. Body() would be calling Statement(), Statement() would call Expression() and so on.

Problem is, in any function the list of symbols could be empty. I thought it was unnecessary to add extra code to each function. Instead I added an try catch to the program.

Isn't it wrong to use an exception this way because if GetSymbol runs out of symbols it is expected behaviour.

Should I avoid throwing the exception?

List<Symbol> symbols;

private void Term()
{...}
private void Expression()
{...}
private void Statement()
{...}
private void Body()
{...}

private Symbol GetSymbol()
{
    if (symbols.Count > 0)

    else
        throw new OutOfTokenException();
}

public void Program()
{
    try
    {
        while (Accept(Symbol.LBRACE))
            Body();
    }
    catch (OutOfTokenException ote)
    {
        Output("Unexpected end of file");
    }
}

}

4 Answers 4

1

You are right - it is bad practice to throw exceptions to handle situations which are fine. This is almost the same as using exceptions for general branching and control flow which is an anti-pattern (here is a Java related question which explains it).

Your GetSymbol() method should return a null if no symbol is found - unless you specifically want an error state when there are no symbols.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I found my answer in your link, although I don't agree with your statement that GetSymbol() should return null.
1

Exceptions denote an exception case to your set of use cases, something which your program does not really expect, but can happen, this is a common situation especially when dealing with user output.

What I am not understanding is how can your list of symbols be empty? If a valid body of text which matches your rules can be empty, then you will need to cater for that scenarios without exceptions.

If on the other hand, an empty set of symbols denotes something which the user should have done but did not, and without it your application cannot function, then, you should throw exceptions.

Comments

1

Try changing your code, so you have a point, where you can check, if there are any symbols left. Like so:

private Symbol GetSymbol()
{
    // just your logic to get symbol
}

private bool SymbolExists()
{
    return this.symbols.Count > 0;
}

And rewrite you calling code to something like:

public void Program()
{
        while (SymbolExists())
        {
            Accept(GetSymbol(Symbol.LBRACE)
            Body();
        }
}

Also think about moving junks of code into independent unit/class, like SymbolReader or so. This way you can pass "symbols" collection as an argument, and validate it before assigning. And you can leave your GetSymbol() implementation as it was original, to make sure proper exception is thrown, when somebody misuses your code (by not calling SymbolExists() before reading).

Comments

0

Your code is lacking some context, e.g. the class declaration which would tell what the public interface of that class is. I'm assuming the GetSymbol(Symbol.LBRACE) call is a mistake, since there is no method with a matching signature.

It seems that the preconditions of the class allow it to call Program at any point, so Program should never throw an exception. Keep in mind that every method has a precondition and a postcondition. If and only if a caller violates a precondition, then you should throw an exception.

Now here's the question: is it allowed to call GetSymbol when there are no symbols to get? The answer determines whether it should throw an exception or not.

But: the use of try/catch obfuscates your intention. That's enough reason to get rid of it. A SymbolExists method like Yura suggested makes your intention perfectly clear.

A bad solution would be to make GetSymbol return null if symbols is empty and check for null in Accept. You'd still get rid of the try/catch, but it obfuscates your intention just as much and is thus just as bad (again IMHO).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.