Bueno tengo este pequeño codigo
class DemoException{
public class ThomasException {
public ThomasException() throws Exception{
throw new Exception();
}
}
class DemoException{
public static void main(String[]args){
int size;
for(size=3;size>-3;size--){
try{
System.out.println("size ="+size);
if(size==0){
throw new ThomasException();
}
int myArray[]=new int[size];
System.out.println(myArray.length);
}
catch(NegativeArraySizeException e){
System.out.println("Negative array" +e.toString());
}
catch(ThomasException e)
{
System.out.println("thomasexception is"+e.toString());
}
}
}
}
Y mi duda esta en que
throw new ThomasException();
pone que es un error como hago para que funcione no explicarme mejor la cuestion esque funcione ese thomasexception como throw como tengo que declararlo para que la clase lo tome ?
Debes de crear tu propia clase de Excepciones.
http://www.sc.ehu.es/sbweb/fisica/cursoJava/fundamentos/excepciones/propias.htm (http://www.sc.ehu.es/sbweb/fisica/cursoJava/fundamentos/excepciones/propias.htm)
Saludos.