未报告的异常Exception;必须被捕获或声明被抛出 ContactDetailBO.validate(splitted[0],splitted[1]);

问题描述 投票:0回答:0

Main.java:14: error: unreported exception Exception;必须被抓住或宣布被抛出 ContactDetailBO.validate(splitted[0],splitted[1]); 它在某些编译器中显示异常,但在某些编译器中它工作正常......我无法理解这里的问题。




import java.io.*;
public class Main {
    public static void main(String[] args)throws Exception{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the contact details");
        String st=br.readLine();
        String[] str=st.split(",");
        try{
       ContactDetailBO.validate(str[0],str[1]);
        
       ContactDetail m1 = new ContactDetail(str[0],str[1],str[2],str[3],str[4]);
       System.out.println(m1);
        }catch(DuplicateMobileNumberException e){
           System.out.println(e);
       }
    }
}

import java.util.*;
public class ContactDetailBO {
    public static void validate(String mobileNumber,String alternateMobileNumber) throws Exception
    {
       if(mobileNumber.equals(alternateMobileNumber)){
           throw new DuplicateMobileNumberException("Mobile number and alternate mobile number are same");
       }

    }
}

public class DuplicateMobileNumberException extends Exception{
    public DuplicateMobileNumberException(String msg){
    super(msg);
    }
}
java exception try-catch
© www.soinside.com 2019 - 2024. All rights reserved.