Try/Catch Block

Definition - What does Try/Catch Block mean?

"Try" and "catch" are keywords that represent the handling of exceptions due to data or coding errors during program execution. A try block is the block of code in which exceptions occur. A catch block catches and handles try block exceptions.

The try/catch statement is used in many programming languages, including C programming language (C++ and C#), Java, JavaScript and Structured Query Language (SQL).

Techopedia explains Try/Catch Block

Try defines a block of statements that may throw an exception. When a specific type of exception occurs, a catch block catches the exception. If an exception is not handled by try/catch blocks, the exception escalates through the call stack until the exception is caught or an error message is printed by the compiler.

A try/catch block also may be nested with one or more try/catch statements. Each try statement has a matching catch statement to handle the exception. If an exception's inner try statement does not have a matching catch statement, subsequent try statement catch handlers are checked. This process continues until all inner try statements are checked for a matching catch statement. If a catch statement does not match, the runtime system handles the exception.

Try/catch block examples include:

  • A try block followed by a catch block
  • A try block followed by one or more catch blocks
  • A try block followed by another try block and then followed by a corresponding catch block
Posted by:

Connect with us

Techopedia on Linkedin
Techopedia on Linkedin
Tweat cdn.techopedia.com
Techopedia on Twitter


'@Techopedia'
Sign up for Techopedia's Free Newsletter!

Email Newsletter

Join 138,000+ IT pros on our weekly newsletter

Resources
Free Whitepaper – Bridging the IT Operations Application Owner Gap
Free Whitepaper – Bridging the IT Operations Application Owner Gap:
Conflicting organizational needs all too often create tension between functional silos. Application owners demand more and more resources.
Free 30 Day Trial – VMTurbo Operations Manager
Free 30 Day Trial – VMTurbo Operations Manager:
VMTurbo's flagship product, Operations Manager maintains your virtual and cloud environments in a healthy state. It manages homogeneous and...