throw and try-catch
throw is used to throw an exception, terminating the current execution flow. try-catch is used to catch and handle exceptions. Functions that throw exceptions do not need to declare them, and callers can choose to catch them or let them propagate upward.
If code in a try block throws an exception, execution immediately jumps to the matching catch block. If the exception is not caught, it continues to propagate up the call stack until it is handled or causes the program to terminate.
The code on the right demonstrates two functions: requireAdult checks age, and divide performs division—both throw exceptions when conditions are not met. The caller uses try-catch to handle these exceptions.
Try removing the first try-catch and observe the effect of an uncaught exception.