[java]compilation erreur exception

Bonjour,
en compilant mon prg exception,j'ai erreur
public class Excep1 
{
 public static void main(String[]args) 
 {
	 int n=Integer.parseInt(arg[0]);
	 int n1=Integer.parseInt(arg[1]);
	 int n2;
	 try
	 {
		 //int n=Integer.parseInt(arg[0]);
		 //int n1=Integer.parseInt(arg[1]);
		 n2=n+n1;
	 }
	 catch(ArithmeticException ex)
	 {
		System.out.println("Arithmetic Exception block1"); 
	 }
	 /*catch(ArithmeticException ex)
	 {
		System.out.println("Arithmetic Exception block2"); 
	 }*/
 }
}

en compilant dans eclipse,j'ai erreurs suivant
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
arg cannot be resolved to a variable
arg cannot be resolved to a variable

at Excep1.main(Excep1.java:6)
en compilant dans l'invite de commande,j'ai erreurs suivant
Except1.java:5:'.class' expected
int n=Integer.parseInt<arg[]^>
Except1.java:6:'.class' expected
int n=Integer.parseInt<arg[]^>

veuillez m'aidez svp

7 réponses

  1. si je comprends bien ta chaine de caracteres s'appelles args? d'ou vient le arg[0] et le arg[1] sans le "s" je veux dire.
    1. j'ai corrigé mon pg mais il s'affiche encore une erreur message
      public class Excep1 
      {
       public static void main(String[]args) 
       {
      	 int n=Integer.parseInt(args[0]);
      	 int n1=Integer.parseInt(args[1]);
      	 int n2;
      	 try
      	 {
      		 n2=n+n1;
      	 }
      	 catch(ArithmeticException ex)
      	 {
      		System.out.println("Arithmetic Exception block1"); 
      	 }
      	 catch(ArrayIndexOutOfBoundsException ex)
      	 {
      		System.out.println("Arithmetic Exception block2"); 
      	 }
       }
      }
      

      Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
      at Excep1.main(Excep1.java:6)

      mais il affiche pas mon erreur de exception
      1. beh on avance alors! et cette erreur c'est à compilation?
    2. oui je veux qu'il prend compte mes lignes d' exception
      est ce que vous avez une idée
      1. je crois que j'ai corrigé est ce que bien ça?
        public class Excep1  
        { 
         public static void main(String[]args)  
         { 
          /*int n=Integer.parseInt(args[0]); 
          int n1=Integer.parseInt(args[1]); 
          int n2;*/ 
          try 
          { 
           int n=Integer.parseInt(args[0]); 
           int n1=Integer.parseInt(args[1]); 
           int n2; 
           n2=n+n1; 
          } 
          catch(ArithmeticException ex) 
          { 
          System.out.println("Arithmetic Exception block1");  
          } 
          catch(ArrayIndexOutOfBoundsException ex) 
          { 
          System.out.println("ArrayIndexOutOfBoundsException block2");  
          } 
         } 
        } 
        
        1. oui cela me parait mieux !
      2. ok,mais pourquoi il s'affiche outofboundsexception
        quelqu'un peut expliquer svp
        1. A mon avis comme t'as pas initialisé ta chaine, il est à null et onc args [0] et [1] ça dépasse la longueur de la chaine
      3. comment je dois passer les argument au lancement du program svp
        1. j'ai essayé dans l'invite de commande

          java Except 1 2

          aucun réponse de retour
          veut dire il a accepté?
          1. c'est ce que je te disais: c'est dans le main qu'il faut que tu passes tes arguments et que t'initialise ta chaine, fais le donne des valeurs concrétes : 1 2 3 ce que tu veux et tu y verras plus clair :)