Try-Catch

Let's learn about try-catch.

What is try-catch?

The try-catch is used in scenarios that might throw an exception. Exception handling is the name used for errors in object-oriented programs, meaning that if there’s an exception within the try section of the code, the program will go directly to the catch block of code. The catch block can specify the type of exception being looked for. In the syntax below, this handle all exceptions.

Syntax

try
{
  // Code that could potentially cause an exception
}
catch (Exception ex)
{
  // Handle the exception
}

Get hands-on with 1200+ tech skills courses.