Exceptions provide a way to react to exceptional circumstances (like runtime errors) in programs by transferring control to special functions called
handlers
.
To catch exceptions, a portion of code is placed under exception inspection. This is done by enclosing that portion of code in a try-block . When an exceptional circumstance arises within that block, an exception is thrown that transfers the control to the exception handler. If no exception is thrown, the code continues normally and all handlers are ignored.
To catch exceptions, a portion of code is placed under exception inspection. This is done by enclosing that portion of code in a try-block . When an exceptional circumstance arises within that block, an exception is thrown that transfers the control to the exception handler. If no exception is thrown, the code continues normally and all handlers are ignored.
An exception is thrown by using the throw
keyword from inside the try
block. Exception handlers are declared with the keyword catch
, which must be placed immediately after the try
block:
| | An exception occurred. Exception Nr. 20 |