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

Creating your own exception class

This lesson covers writing a custom exception class in VB.NET, with the aim of expressing kinds of error more specifically.

Inherits Exception creates a custom exception class from the standard one. Use it when you want an application-specific error — a failed input check, say — to have a name of its own.

In the sample code, a ValidationError class inherits Exception, and CheckAge raises it with Throw New ValidationError(...) when the age is below zero. Catch e As ValidationError then catches just that kind.

A common early stumble is sticking to the standard Exception and losing the ability to tell errors apart. A custom class lets the Catch side respond differently per kind of error.

In professional work, it is usual to define errors that matter to the business — validation failures, permission errors — as their own exception classes, which makes error handling far clearer.

๐Ÿ“– 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)