Join David Gassner for an in-depth discussion in this video Understanding syntax errors vs. runtime exceptions, part of Java 8 Essential Training.
- Dealing with bugs is a common part of all software development. Before we get to much further into the Java programming language, I'll describe how to handle various kinds of errors. Both syntax errors, which you must deal with before you can compile and run your application, and run time exceptions. Those exceptions that occur while the application is running. In the empty made method, I'll start by introducing a syntax error. If you've been coding for any amount of time, I'm sure you've seen some of these.
I'll declare a String that I'll name s. And I'll try to assign it to the keyword Null. Now, I'll intentionally misspell it. The keyword Null in Java is all lower case. When I type it with an upper case in, and then I try to run the application, the application doesn't even compile. Instead I get a syntax error, telling me that the compiler can't find the symbol. It means this keyword Null. And it tells me exactly where the problem is and how to deal with it.
In a larger application that might have a lot of different Java classes, you might run into a situation where you don't even know where the file is. But IntelliJ IDEA gives you a very simple way of finding it. If you see these squeegee lines, that means there's a syntax error somewhere. And you can go to the Project window and look at the Problems view. And you'll see a listing of all files that have syntax errors. You can then double-click on the file, and then press the F2 key on either Mac or Windows.
And the cursor will jump to the next error. Frequently IntelliJ IDEA will even know how to fix it. If you see the light bulb icon, you can pull down the list, and see if there's a solution. In this case, I know that the error is that the keyword is Null with a lower case n. So I'll just manually fix it myself. So that's a syntax error. And again, IngelliJ IDEA gets information from the Java compiler, and then tells you where your errors are, and to whatever extent possible, gives you hints on how to fix it.
So, once you fix all of your syntax errors, you're then able to compile and run the application. But even then, it might not solve all the issues. For example, I'll try to output this String to the console. I can find out whether I can build the application by going to the menu and selecting Build, and then Make Project. Down in the low left corner, if I see the message Compilation Completed Successfully, that means that I don't have any syntax errors.
Notice that my Problems view is empty. But now I'll try to run the application, and I get something on the console. Just the keyword null. That in itself is not enough to cause an actual exception. But lets put in some code that will cause an exception. I'll create another stream named welcome, and I'll give it a value of Welcome! with an exclamation mark at the end. Then, as I've done previously, I'll create an Array of characters that I'll name chars.
And I'll get the value from welcome.toCharArray. So now, I have a set of characters in a particular order. Next, lets say that I want to get the last character in the Array. So I'll create a char variable. And I'll name it lastChar. And I'll get its value using Array syntax. I'll start with the name of the Array, and then pass in chars.length. Length in this case is a property, not a method, so you don't need the parenthesis at the end.
Then, I'll output that character to the console. I'll wait a moment, and wait for any squeegee lines, I don't see any. And I also look at my Problems view and don't see anything listed there. So I don't have any syntax errors. But when I try to run the application, I get this message: Exception in thread "main". And it tells me what kind of exception it is. This is something called an ArrayIndexOutOfBoundException. I'll get more deeply into exception handling in a couple of videos.
But what's important to know right now, is that when you hit this kind of exception, it stops your application in its tracks. Its fatal, and the application can't continue. So the problem here is in this code. I tried to reference the character at an index position, where the index is the same number returned by the length. But remember that I've said, that indexing in Arrays starts at zero. So the last character has an index that's one less than the length of the Array.
To fix this code, I'll ask for the character at the index position of the Array's length minus one. And then I'll run the code again. And this time it works. I display the exclamation mark that's at the end of this String. So, that's how exceptions can be generated. If you right-code that's syntactically correct, but where the logic is wrong, frequently you'll get this kind of exception. And understanding how to look for it, how to find out where it's happening, and then how to fix it, is critical to good Java programming.
- Understanding the history and principles of Java
- Installing Java, IntelliJ IDEA, and BlueJ
- Creating a Java project
- Working with variables, values, and expressions
- Working with object data types
- Building, comparing, and parsing strings
- Debugging and exception handling
- Creating loops and reusable code
- Passing arguments by reference or value
- Using simple and complex arrays
- Creating custom classes
- Understanding inheritance and polymorphism
- Managing files with Java libraries
- Documenting code with Javadoc
- Packaging classes in JAR files