C++ error handling
JavaScript Error Handling
Src
Exceptions Handling
An exception is thrown when a fundamental assumption of the current code block is found to be false.
try, catch, and finally
statements enable the use of a resource, catching any possible exceptions and then releasing the resource in the final block, making sure that there are no memory leaks.
Try-Except Clause
The try
statement works as follows.
- First, the try clause (the statement(s) between the
try
andexcept
keywords) is executed. - If no exception occurs, the except clause is skipped and execution of the
try
statement is finished. - If an exception occurs during execution of the
try
clause, the rest of the clause is skipped. Then, if its type matches the exception named after theexcept
keyword, the except clause is executed, and then execution continues after the try/except block. - If an exception occurs which does not match the exception named in the except clause, it is passed on to outer
try
statements; if no handler is found, it is an unhandled exception and execution stops with a message as shown above. - An except clause may name multiple exceptions as a parenthesized tuple, for example:
except (RuntimeError, TypeError, NameError):
The try
… except
statement has an optional else clause, which, when present, must follow all except clauses.
It is useful for code that must be executed if the try clause does not raise an exception. For example:
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
Example
Exception Chaining
Finally Clause
Src
The finally
clause will execute as the last task before the try
statement completes. The finally
clause runs whether or not the try
statement produces an exception.
- If an exception occurs during execution of the
try
clause, the exception may be handled by anexcept
clause. If the exception is not handled by anexcept
clause, the exception is re-raised after thefinally
clause has been executed. - An exception could occur during execution of an
except
orelse
clause. Again, the exception is re-raised after thefinally
clause has been executed. - If the
finally
clause executes abreak
,continue
orreturn
statement, exceptions are not re-raised. - If the
try
statement reaches abreak
,continue
orreturn
statement, thefinally
clause will execute just prior to thebreak
,continue
orreturn
statement’s execution. - If a
finally
clause includes areturn
statement, the returned value will be the one from thefinally
clause’sreturn
statement, not the value from thetry
clause’sreturn
statement.
As you can see, the finally
clause is executed in any event. The TypeError
raised by dividing two strings is not handled by the except
clause and therefore re-raised after the finally
clause has been executed.
In real world applications, the finally
clause is useful for releasing external resources (such as files or network connections), regardless of whether the use of the resource was successful.
User-defined Exceptions
Src
Programs may name their own exceptions by creating a new exception class (see Classes for more about Python classes).
Exceptions should typically be derived from the Exception
class, either directly or indirectly.
More on Python OOP