1

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);}
0

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

2

You would need to catch your specific exception:

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

See https://beginnersbook.com/2013/04/try-catch-in-java/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.