55

How should an exception be raised in VB.NET?

3
  • 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, 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. Oct 31, 2012 at 12:39
  • 3
    I google this question every time I have to write this code. Glad it's here.
    – bendecko
    Oct 10, 2018 at 16:59

1 Answer 1

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.")
5
  • @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.
    – Andrea_86
    Sep 18 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.