[java]cannot be resolved to a type

domxaline -  
 domxaline -
Bonjour,
program suivant en compilant s'affiche une erreur
cannot be resolved to a type
veuillez m'aidez svp
public class Excep3 
{
 public static void main (String args[])
 {
	 
		if(args[0]=="hello")
			System.out.println("String is right");
		else
		try
		{
			throw new MyException("invalid String");
		}
	 
		catch(MyException ex)
		{
			System.out.println(ex.getmessage());
		}
		
	 }
 }

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
MyException cannot be resolved to a type
MyException cannot be resolved to a type

at Excep3.main(Excep3.java:12)

3 réponses

  1. domxaline
     
    j'ai corrigé ainsi
    import java.lang.*;
    class MyException extends Exception
    {
       MyException(String message)
       {
         super(message);	
       }
    }
    public class Excep3 
    {
     public static void main (String args[])
     {
    	 
    		if(args[0]=="hello")
    			System.out.println("String is right");
    		else
    		try
    		{
    			throw new MyException("invalid String");
    		}
    	 
    		catch(MyException ex)
    		{
    			System.out.println(ex.getmessage());
    		}
    		
    	 }
     }
    

    maintenant j'ai erreur suivante:
    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The method getmessage() is undefined for the type MyException

    at Excep3.main(Excep3.java:24)
    0
    1. kalamit Messages postés 307 Statut Contributeur 17
       
      Salut,

      Ah, les exceptions, on peut en écrire des bouquins.

      1. C'est crapouillaud de tout mettre au même endroit. Créée une classe MyException à part.
      2. Ton code est opérationnel, tu dois avoir un problème de path. Chez moi, ca compile et ça s'execute.
      0
    2. KX Messages postés 19031 Statut Modérateur 3 020
       
      "1. C'est crapouillaud de tout mettre au même endroit."
      Je ne vois pas le problème ! Si encore ça avait été une inner classe... mais ce n'est pas le cas !

      "2. Ton code est opérationnel, tu dois avoir un problème de path."
      Si tu ne dis pas à domxaline qu'il faut mettre un M majuscule à getMessage, il va lui falloir 2h pour trouver !
      0
  2. domxaline
     
    j'ai essayé dans le cmd.exe
    toujours il dit "invalid string"
    c:\users\pierre\java Excep3 bonjour
    invalid String
    c:\users\pierre\java Excep3 hello
    invalid String
    c:\users\pierre\java Excep3 "hello"
    invalid String
    
    0
    1. KX Messages postés 19031 Statut Modérateur 3 020
       
      Plutôt que de comparer deux String avec == il faut utiliser la méthode equals !
      Mais bon ce genre d'erreurs, il faudrait que ça rentre...
      0