In Python the [try … 
except](https://docs.python.org/3/tutorial/errors.html#handling-exceptions) 
statement has an optional `else` clause. It is useful for code that should only 
be executed if the try clause does not raise an exception.

In Nim (like in C++) we have to write:
    
    
    try:
      # Line which might raise an exception.
      # Potentially lots of
      # other lines which
      # should only be
      # executed if no
      # exception was raised.
    except:
      discard
    
    
    Run

with `else` this would read:
    
    
    try:
      # Line which might raise an exception.
    except:
      discard
    else:
      # Potentially lots of
      # other lines which
      # should only be
      # executed if no
      # exception was raised.
    
    
    Run

The use of the `else` clause is better than adding additional code to the `try` 
clause because it avoids accidentally catching an exception that wasn't raised 
by the code being protected by the `try … except` statement.

IMO it also makes it clearer in what code an exception is expected to happen.

Do you think an optional `else` clause would be worth adding to Nim?

Reply via email to