Java URL读取:在main中工作,但在构造函数中不工作

Java URL读取:在main中工作,但在构造函数中不工作,java,Java,我正在开发一个基于Alphavantage API的软件。我为每只股票设计了一个对象,它将有一个存储股票历史价格的ArrayList。为此,我读取了一个url,该url将我引导到一个cvs文件,我可以从中提取所需的数据。这在主函数中有效,但在构造函数中无效。对于公共股票(String-ticker),抛出异常 error: unreported exception Exception; must be caught or declared to be thrown Stoc

我正在开发一个基于Alphavantage API的软件。我为每只股票设计了一个对象,它将有一个存储股票历史价格的ArrayList。为此,我读取了一个url,该url将我引导到一个cvs文件,我可以从中提取所需的数据。这在主函数中有效,但在构造函数中无效。对于
公共股票(String-ticker),抛出异常

 error: unreported exception Exception; must be caught or declared to
 be thrown
         Stock msft = new Stock("MSFT");
没有
抛出异常
我得到

 error: unreported exception MalformedURLException; must be caught or
 declared to be thrown
         URL url = new URL(link);
我真的不明白我的代码出了什么问题。有人能帮我吗?以下是完整的源代码:

import java.net.*;
import java.io.*;
import java.util.ArrayList;
import java.lang.Math.*;

public class SmartBeta {

    public static void main(String[] args) {

        Stock msft = new Stock("MSFT");
    }
}

class Stock{
    //ArrayList to store prices
    private ArrayList<Double> prices = new ArrayList<Double>();

    //stock constructor
    //the argument takes the ticker symbol, in order to find the 
    //corresponding data
    public Stock(String ticker){
        String link = "https://www.alphavantage.co/query?function=TIME_SERIES_WEEKLY_ADJUSTED&symbol="+ticker+"&apikey=PRIVATE_KEY&datatype=csv";
        URL url = new URL(link);
        String cvsSplitBy = ",";
        URLConnection yc = url.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
        String inputLine;
        int i = 0;
        //we read the csv file returned, seperate it by commas, convert the 
        //numbers into doubles and store them in the ArrayList
        while ((inputLine = in.readLine()) != null) {
            if (i == 0) {
                ++i;
                continue;
            }
            String[] data = inputLine.split(cvsSplitBy);
            prices.add(Double.parseDouble(data[5]));
            ++i;
        }
        in.close(); 
        System.out.println(prices);   
    }

}
import java.net.*;
导入java.io.*;
导入java.util.ArrayList;
导入java.lang.Math.*;
公共类智能测试版{
公共静态void main(字符串[]args){
股票msft=新股票(“msft”);
}
}
类别股票{
//ArrayList以存储价格
私有ArrayList价格=新ArrayList();
//股票构造师
//参数采用股票代码,以便查找
//相应数据
公开股{
字符串链接=”https://www.alphavantage.co/query?function=TIME_SERIES_WEEKLY_ADJUSTED&symbol=“+ticker+”&apikey=PRIVATE\u KEY&datatype=csv”;
URL=新的URL(链接);
字符串cvsSplitBy=“,”;
URLConnection yc=url.openConnection();
BufferedReader in=新的BufferedReader(新的InputStreamReader(yc.getInputStream());
字符串输入线;
int i=0;
//我们读取返回的csv文件,用逗号分隔,转换
//将数字转换为双精度并存储在ArrayList中
而((inputLine=in.readLine())!=null){
如果(i==0){
++一,;
继续;
}
字符串[]数据=inputLine.split(cvsSplitBy);
prices.add(Double.parseDouble)(数据[5]);
++一,;
}
in.close();
系统输出打印项次(价格);
}
}

您必须声明库存构造函数,可能会引发IOException,因此在签名中添加异常声明:

public Stock(String ticker) throws IOException {...}
然后,在主方法中处理此异常:

    public static void main(String[] args) {

    try {
        Stock msft = new Stock("MSFT");
    } catch (IOException e) {
        //exception - do something
        e.printStackTrace();
    }
}

您必须声明股票构造函数,可能会引发IOException,因此在签名中添加异常声明:

public Stock(String ticker) throws IOException {...}
然后,在主方法中处理此异常:

    public static void main(String[] args) {

    try {
        Stock msft = new Stock("MSFT");
    } catch (IOException e) {
        //exception - do something
        e.printStackTrace();
    }
}

您只需在新URL(链接)周围添加一个try/catch。只需在外部声明url变量,并将其赋值为null,然后在try/catch中将其赋值给url对象。在使用它之前,您还需要检查它是否为null。如果一个方法引发异常,那么这意味着调用代码必须捕获该异常。您必须处理该异常。你应该研究一下。你只需要在你的新URL(链接)周围放一个try/catch。只需在外部声明url变量,并将其赋值为null,然后在try/catch中将其赋值给url对象。在使用它之前,您还需要检查它是否为null。如果一个方法引发异常,那么这意味着调用代码必须捕获该异常。您必须处理该异常。您应该研究。或者可能只是一个
MalformedURLException
。MalformedURLException扩展IOException。当您创建新的URL对象时,会引发该事件。但在这种情况下,您还需要处理来自连接和流的异常,这两种异常都会引发IOException;那么IOException就是正确的了。或者可能只是一个
MalformedURLException
。MalformedURLException扩展了IOException。当您创建新的URL对象时,会引发该事件。但在这种情况下,您还需要处理来自连接和流的异常,这两种异常都会引发IOException;那么,IOException才是正确的选择。