Ad space (banner)
๐Ÿ”ทVB.NET Lessons
Lesson 10 / 68

Error handling (Try...Catch)

This lesson covers error handling with VB.NET's Try...Catch, with the aim of keeping a program running even when something unexpected goes wrong.

Try / Catch / End Try catches an error so the whole program does not stop. Without error handling, the moment unexpected input arrives the program halts — preparing in advance matters.

The sample code divides by zero inside a Try block and catches only that particular kind of error with Catch ex As DivideByZeroException. Naming the kind of error keeps you from silently swallowing errors you did not anticipate.

A common early stumble is not thinking in advance about which exceptions can occur. Working that out is an important design step towards a program that runs reliably.

Swallowing an exception and doing nothing makes the cause hard to find later, so at minimum leaving a log entry is recommended. Real applications treat this as important.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run VB.NET directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)