[java]cannot be resolved to a type

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. 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)
    1. Contributeur
      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.
    2. Modérateur
      "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 !
  2. 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
    
    1. Modérateur
      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...