How should an exception be raised in VB.NET?
Asked
Active
Viewed 1.1e+01k times
55
-
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
-
3I google this question every time I have to write this code. Glad it's here. – bendecko Oct 10 '18 at 16:59
1 Answers
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
-
@JonathonReinhart, understood. If this was a duplicate, then close it as such, if not it should be answered, correct? – Adriaan Stander Oct 31 '12 at 04:38
-
I apologize, and rescind my downvote. How can this question not exist on StackOverflow?! – Jonathon Reinhart Oct 31 '12 at 04:41
-
@JonathonReinhart, I think it might actually be that trivial that it might be the lesson after "Hello World". X-) – Adriaan Stander Oct 31 '12 at 04:43
-
I think you might be right. Add an example and I will (still surprised) upvote :-) – Jonathon Reinhart Oct 31 '12 at 04:44