Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
139 views
in Technique[技术] by (71.8m points)

java - Why can I not create a custom exception class?

I'm trying to create a custom exception for my program but when go to extend the exception class i get two errors

I created a method called fileNotFoundException(String m) and i tried to put super(m); inside of it but when I do that it gives me the error "Constructor call must be the first statement in a constructor". But when I do it outside the method it's fine. Also when I create the method they make me put void. I'm watching a video about creating custom exception errors and they had none of those problems. They didn't have to put a return type and they were able to put super() inside of the method with no problem.

What am i doing wrong that makes it throw errors like that?

code:


public class WrongFileSelcException extends Exception {
    
    public fileNotFoundException(String m) {
        super(m);
        
    }

}

video: https://www.youtube.com/watch?v=KFoN6hQ3yAs

question from:https://stackoverflow.com/questions/65947121/why-can-i-not-create-a-custom-exception-class

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The constructor must take the same name as the class to which it belongs. You need:

public class WrongFileSelcException extends Exception {

  public WrongFileSelcException(String m) {
      super(m);
    
  }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...