ArrayIndexOutOfBounds exception in main method

后端 未结 4 1240
不思量自难忘°
不思量自难忘° 2021-01-25 18:53

I am getting array bound error but to my mind, array starts from 0, so what is wrong with this code?

public class Quadratic {

    public static void main(String         


        
4条回答
  •  南方客
    南方客 (楼主)
    2021-01-25 19:13

    ArrayIndexOutOfBounds exception occur when you are trying to extract the variable at a wrong position in the array.

    When you run a program in java after compilation e.x.javac hello and then executing the class file by java hello , the 0th position of the args[0] will be the filename i.e. hello in this case.

    For you to successfully extract argument issued at the run time you have to use n+1 position in the array. For example. If I am running my program as java hello argument I'll be using args[1] if I wish to use the issued variable argument at run time and so on for extracting the associated text.

    If you are unsure whether your statement will generate an exception or not, it is always safe to use try & catch block .Read - Exception Handling

提交回复
热议问题