[java]cannot be resolved to a type

Fermé
domxaline - 17 sept. 2012 à 23:38
 domxaline - 18 sept. 2012 à 17:42
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

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
kalamit Messages postés 278 Date d'inscription samedi 10 juin 2006 Statut Contributeur Dernière intervention 29 juin 2016 17
18 sept. 2012 à 10:14
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
KX Messages postés 16668 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 17 mars 2023 3 005
18 sept. 2012 à 14:06
"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