Capture argument error

1

I have the following program:

public static void veureinfo(String filePath) throws FileNotFoundException{

      File origen = new File(filePath);


      if (origen.isFile()){
       System.out.println("Java VeureInfo " +origen.getAbsolutePath());
       System.out.println("INFORMACIÓ: Informació sobre el fitxer:");
       System.out.println("Nom del fitxer : "+origen.getName());
       System.out.println("Ruta : "+origen.getPath());
       System.out.println("Ruta Absoluta : "+origen.getAbsolutePath());
       System.out.println("Es pot escriure : "+origen.canWrite());
       System.out.println("Es pot lleguir : "+origen.canRead());
       System.out.println("Grandaria : "+origen.length()+(" bytes"));

      }else{

         System.out.println("directorio") ;
      }

   }


public static void main(String args[]) {


    try {
        veureinfo(args[0]);
    } catch (FileNotFoundException error) {
        System.out.println(error);
    }
}
}

The doubt that I have is, if I leave empty the space of the arguments is to say if I do not put any error skips me:

  

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0     at veureinfo.VeureInfo.main (VeureInfo.java:35)   C: \ Users \ Montse \ AppData \ Local \ NetBeans \ Cache \ 8.1 \ executor-snippets \ run.xml: 53:   Java returned: 1 BUILD FAILED (total time: 0 seconds)

How can I capture this error?

    
asked by Montse Mkd 21.09.2017 в 17:56
source

1 answer

1

This may help.

public class Pruebatry {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here

    if (args.length==0) {
        try {
            throw new Exception("Error!! No hay nada!!");
        } catch (Exception ex) {
            Logger.getLogger(Pruebatry.class.getName()).log(Level.SEVERE, null, ex);
        }
    }else{
        System.out.println("Si paso el código");

    }
}

}

If you wanted more checks you would only have to chain them before checking your arguments.

Update This way it does not freeze.

public class Pruebatry {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here

    if (args.length==0) {
        try {
            throw new Exception("Error!! No hay nada!!");
        } catch (Exception ex) {
            System.out.println("La liaste compadre!! Captura un argumento!");
            operacionQueUseScannerParaVolverAIntentarlo();
        }
    }else{
        System.out.println("Si paso el código");

    }
}

}

Actually it's very similar, just delete the logger and execute the operations that I want.

MORE UPDATED

If you do not want to send an exception, you do not need to put it (For the case of 0 arguments) since you do not want it to freeze. I recommend the following:

public class Pruebatry {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here

    if (args.length==0) {
            System.out.println("La liaste compadre!! Captura un argumento!");

    }else{
        System.out.println("Si paso el código");

    }
}

}

For the following cases you want to check just chain the ifelse and you will get your validations according to your requirements.

    
answered by 21.09.2017 / 18:09
source