Try Catch

During program execution an error can be thrown. Instead of ending the program an error can be caught and a certain code can be executed.

Error Throwing And Handling

Syntax for handling error:

Let's look at the following example:

Output:

Since an error is thrown in the try block, the code from the catch block is called.

Handling Runtime Exceptions

Such example is division by zero:

Output: