How should an exception be raised in VB.NET?
-
2@JonathonReinhart: I don't think there is anything wrong with having a definitive answer to a question like this on this site.– CJ7Oct 31, 2012 at 5: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 ReinhartOct 31, 2012 at 12:39
-
3I google this question every time I have to write this code. Glad it's here.– bendeckoOct 10, 2018 at 16:59
1 Answer
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.")
-
@JonathonReinhart, understood. If this was a duplicate, then close it as such, if not it should be answered, correct? Oct 31, 2012 at 4:38
-
I apologize, and rescind my downvote. How can this question not exist on StackOverflow?! Oct 31, 2012 at 4:41
-
@JonathonReinhart, I think it might actually be that trivial that it might be the lesson after "Hello World". X-) Oct 31, 2012 at 4:43
-
I think you might be right. Add an example and I will (still surprised) upvote :-) Oct 31, 2012 at 4:44
-
is it possible to manage an exception from the caller function? Ex: Function A call Function B. I force function B to raise an exception but i want to manage it in the try catch of function A. Sep 18 at 12:47