Join David Gassner for an in-depth discussion in this video Common runtime errors, part of Visual Basic Essential Training (2013).
As I've previously described, there are two broad categories of errors, compile time and Runtime errors. A Runtime error is an error that won't prevent you from building and testing your application, but can cause the application to crash depending on the nature of the application you're working in. Runtime errors can evolve from lots of causes. And I can't describe all the different kinds of errors that can happen, but I can show you two common errors here. I'm working in the project runtime errors and I'll start by declaring a new variable data type as a byte.
I'll say Dim b As Byte, and I'll sign an initial value of 255. The Byte class is an unassigned data type. That is, it can only contain positive values, and specifically with byte, it can zero to 255. I've placed its code into calculate subroutine, so it'll be called whenever I click any of the buttons in my calculator app. I'll run the application, enter some values, and click a button and everything's fine but now lets see what happens when I add one to the value of b.
This will exceed the available range of the byte data type. I'll b equals b plus 1, making it 256. First I'll run the application without debugging by pressing control F5. I'll enter some values and click a button, and I get the mysterious message that the application has stopped working. I'll click Cancel, and return to Visual Studio. This is what the end-user would see, if you go ahead and publish the application, without fixing this error. To find out more about the nature of the error, run the application with debugging.
So this time, I'll press F5. Once again, I'll enter some values and click a button. This time I get the unhandled exception dialog box. This dialog box will tell you a little bit more about the nature of the exception. In this case, I get something called System.OverflowException, and an error message of Arithmetic operation resulted in an overflow. You could click Continue, meaning you want to ignore the error, but it's better to click Break. That will take you back to your code in Visual Studio and show you which line of code triggered the runtime exception.
You should see that there were a couple of panels that you might not have noticed before. One called Locals and one called Call Stack. If you don't see these panels, go to the menu right now, and choose Window > Reset Window Layout. And click Yes, and they should show up. The Call Stack panel will give you a list of the functions or subroutines that have been called to get to this point. I'm going to drag this up, and then expand it, and show that I'm in the calculate subroutine, but that was called from b, t, n, add click. Watching the Call Stack, or the order of the calls, can help you figure out where your error might be, so you can make fixes.
I'm going to reset my Window Layout again. And then, get out of debugging by pressing Shift+F5. And I'm going to comment out these two lines of code, because I'd like to show you another runtime exception. In this version of my application in runtime errors, I've added a new constant called IntDivide. And I've added a new button in the XAML file that has the back slash operator. That's the integer division operator. In the XAML file, with the button selected, I'll go to the Properties panel and click the little lightening bolt button to show my event handlers. And then, that shows me that the click event for this button is calling a method called btnIntDivide click.
I'll double-click there and I jump to that sub routine in my vb file. Now here's what I want to do. I want to call the calculate method and pass in that new constant of IntDivide. So, I'll say calculate IntDivide. Next, I'll go to the calculate method. I'll select it, right-click and go to the definition. Then, I'll scroll down a ways to my select case where I'm looking at the operation, and I'm going to add a new case statement. I'll say case IntDivide, and for that case, I'll recalculate the result and I'll use the backslash integer division operator.
Result equals dbl1 \ dbl2. Now, I'll run the application with debugging by pressing F5. And I'll enter a couple of values. I'll start with values of 13 and 5. And then, click my integer division button, and I get 2, the integer value of the result. This operation, discards the remainder and only returns the integer. But now, watch what happens when I try to do integer division, and I enter 0. If I do regular division, I get my error, Divide by zero.
But integer division gives me an unhandled exception. A runtime error. And the nature of this exception, is a class called System.DivideByZeroException. For some reason in Visual Basic, you'll only see this exception with integer division, not with standard division, which executes the operation and returns one of the infinity values, as I've described previously. But here, I'm getting a runtime exception, or a runtime error, and the message is attempted to divide by zero. So, this is what runtime exceptions look like.
Both when the users running them, when the application can simply crash or when you're running them with debugging as the developer. Once you figure out that it's possible to hit a runtime exception, you must fix it in your code. And I'll show you some ways of doing that using the tri-catch architecture in a later movie.
- What is Visual Basic?
- Learning Visual Basic syntax
- Declaring variables and data types
- Working with numbers, Boolean values, and dates
- Using strings, words, and characters
- Repeating blocks of code with loops
- Evaluating conditions with if, then, and else
- Debugging and handling exceptions
- Managing ordered data with arrays
- Managing application logic with modules
- Defining custom classes
- Storing data with instance fields