55

How should an exception be raised in VB.NET?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
CJ7
  • 22,579
  • 65
  • 193
  • 321
  • 2
    @JonathonReinhart: I don't think there is anything wrong with having a definitive answer to a question like this on this site. – CJ7 Oct 31 '12 at 05:05
  • Sure. But it blows my mind that after the X years this site has been in existence no one has asked this question. As astander mentioned below, it's probably because it is so trivial and easily answered elsewhere. – Jonathon Reinhart Oct 31 '12 at 12:39
  • 3
    I google this question every time I have to write this code. Glad it's here. – bendecko Oct 10 '18 at 16:59

1 Answers1

82

You would throw a new exception.

Have a look at Throw Statement (Visual Basic)

The Throw statement throws an exception that you can handle with structured exception-handling code (Try...Catch...Finally) or unstructured exception-handling code (On Error GoTo). You can use the Throw statement to trap errors within your code because Visual Basic moves up the call stack until it finds the appropriate exception-handling code.

EDIT

By request and from the link

Throw New System.Exception("An exception has occurred.")
Adriaan Stander
  • 162,879
  • 31
  • 289
  • 284