-1

I'm struggling figuring out something about checked Exception in Java. What I've learned (I may be wrong) is that if you throw a checked Exception you have two options.

  1. you have to catch it with try catch block
  2. you delegate the caller with the keyword throws

In the code down below I'm going with the first option, I'm trying to catch the exception in the Main class but I get the "exception PersonaException is never thrown in body of corresponding try statement" as if I never try to catch it. What am I missing?

public class Main{
public static void main(String args[]){
    Persona p = new Persona("Michele", "Bianchi");
    Scanner s = new Scanner(System.in);
    System.out.println("Inserisci un numero di telefono: ");
    String tel = s.nextLine();
    
    try{
        p.setTel(tel);
    }
    catch(PersonaException e){
        System.out.println(e.toString());
    }

    System.out.println("Ciao");
    
    System.out.println(p.toString());

}

}

public class Persona{
private String nome;
private String cognome;
private String tel;

public Persona(String nome, String cognome){
    this.nome = nome;
    this.cognome = cognome;
}

public void setTel(String tel){
    if(tel.length()!=10){
        throw new PersonaException("Il numero di telefono è errato");
    }
    else
        this.tel = tel;
    
}

public String getTel(){
    return tel;
}

public String toString(){
    return nome+" "+cognome+": "+tel;
}

}

public class PersonaException extends Exception{
public PersonaException(String msg){
    super(msg);
}

}

2
  • 2
    Your two options apply to a single scope - meaning since you don't try-catch in setTel you must add throws - Persona class likely does not compile (it didn't when I tried it). Commented Jul 2, 2021 at 16:22
  • But I tried to catch it in the Main class, what am I missing. Thank you for your reply. Commented Jul 3, 2021 at 16:18

2 Answers 2

0

What you're missing is that you wrote invalid java code.

Java's linking phase doesn't care about the contents of any method anywhere. Intentionally. Code works one way today; perhaps tomorrow somebody wants you to change things a bit, or you need to fix a bug. Thus, analysis of your code is irrelevant, it's all about the signatures.

Thus, when compiling:

try {
   bunchOfStatementsHere();
} catch (CheckedException e) { .. }

javac looks at all signatures of all the things you invoke in that try block, but not at the code of all the things you invoke there.

In other words, if bunchOfStatementsHere is setTel("someTel");, java looks at the signature of the setTel method and not at the code body!

The signature of your setTel method indicates it doesn't throw anything. It's defined as public void setTel(String tel) - that part is the signature, all the stuff in the {} that follows is the body.

No throws clause here.

Of course, this also means your Persona.java file doesn't compile - you can't throw new X(); where X is a checked exception, unless you catch it (which you don't), or put it in your throws clause (which you didn't).

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

2 Comments

"unless you catch it (which you don't)", I don't get it, I'm trying to catch it in the Main class. What am I missing? Thank you for your reply
Your setTel method's signature does not declare that it throws PersonaException. Its body does throw it (and doesn't catch it). Your code doesn't compile. First get your code to compile. Learn to walk before you run, and all that.
0

You indeed must declare or handle any Exception, unless it's a RuntimeException. but you must also do this where you create the exception:

public void setTel(String tel) throws PersonaException{
        if(tel.length()!=10){
            throw new PersonaException("Il numero di telefono è errato");
        }
        else
            this.tel = tel;

    }
````

if you dont declare in the signature of setTel, you must handle it here (in a try-catch), which would of course be pointless here.

1 Comment

I'm handling it in the Main class with a try catch. What am I missing? Thank you for your reply

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.