Errors vs runtime Exceptions in Java

Errors vs runtime Exceptions

Debug syntax errors vs runtime exceptions -  Dealing with bugs is a common part of all software development. Both syntax errors, which you must deal with before you can compile and run your application and runtime exceptions, those exceptions that occur while the application is running. In the empty main 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 key word, no. Now I'll intentionally misspell it. Errors vs runtime Exceptions in Java The keyword no in Java is all lower case. When I type it with an uppercase N 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 key word, no. 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 squiggly 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 curse will jump to the next error. Frequently, IntelliJ IDEA will even know how to fix it. Errors vs runtime Exceptions in Java 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 key word is no with a lowercase N. So I'll just manually fix it myself. So that's a syntax error. And again, IntelliJ 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 fixed 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 lower left corner, if I see the message compilation completed successfully that means that I don't have any syntax errors and noticed that my problems view is empty. But now I'll try to run the application and I get something on the console. Errors vs runtime Exceptions in Java Just the key word, no. That in itself is not enough to cause an actual exception but let's put in some code that will cause an exception. I'll create another string 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 are named chars and I'll get the value from welcome.toCharArray. So now I have a set of characters in a particular order. Next, let's say that I want to get the last character in the array. So I'll create a char variable and I'll name it last Char 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 parentheses at the end. Then output that character to the console. I'll wait a moment and look for any squiggly lines. I don't see any. Errors vs runtime Exceptions in Java 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 array index out of bounds exception. When you hit this kind of exception, it stops your application in its tracks it's 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. error in java 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 erased 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 the string. So that's how exceptions can be generated. If you write 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 programing.