Dificuldades de gerar IllegalArgumentException

1 resposta
A

pode me ajudar a gerar IllegalArgumentException e IllegalStateException?

/**
* Método main que lança java.lang.ClassCastException
	 * @param args
	 */
	public static void main(String[] args) {
		TestarExcecoes t = new TestarExcecoes();
		new TestarExcecoes().metodoGeraIllegalArgumentException(t);
	}
	
	TestarExcecoes metodoGeraIllegalArgumentException(TestarExcecoes t1){
		if (t1.equals( this )){
			throw new IllegalArgumentException("Objeto diferente!");
		}
		else
			System.out.println("Ok");
		return t1;
	}

1 Resposta

ViniGodoy

Se vc gera 2 objetos diferentes, você não vai entrar no seu if:

public static void main(String[] args) {  
     TestarExcecoes t = new TestarExcecoes();  
     t.metodoGeraIllegalArgumentException(t);  
 }  
   
 TestarExcecoes metodoGeraIllegalArgumentException(TestarExcecoes t1){  
     if (t1.equals( this )){  
         throw new IllegalArgumentException("Objeto diferente!");  
     }  
     else  
         System.out.println("Ok");  
     return t1;  
 }

Outra possibilidade (que parece fazer mais sentido), é colocar um ! no seu if.

Criado 20 de setembro de 2010
Ultima resposta 20 de set. de 2010
Respostas 1
Participantes 2