Java exception variable

Viewed 61

Is there any way to access exception variable lenght in catch space?

    public class myexception extends Exception{
        public int lenght;
        myexception (int a){
            this.lenght=a;
        } 
}

try{
       if(something) throw new myexception (10);
} 
catch(Exception e){System.out.println(e.lenght);}
2 Answers

No, because e is of type Exception and not myexception, so it would not have any way of knowing what lenght is.

If you want that, then catch myexception instead of Exception.

Related