如何在 Java 中为 url 对象定义显式构造函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9852424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 22:38:43  来源:igfitidea点击:

How to define an explicit constructor for an url object in Java

javaconstructorprocessing

提问by Collin

I am defining a url in processing (or java) with the line:

我正在使用以下行定义处理(或 java)中的 url:

URL base = new URL("http://www.google.com/");

So I get the following error: Default constructor cannot handle exception type MalformedURLException thrown by implicit super constructor. Must define an explicit constructor. Which I assume is because if my url isn't valid, there's no corresponding catchto catch the error (because I'm declaring my url outside of try).

所以我收到以下错误:默认构造函数无法处理隐式超级构造函数抛出的异常类型 MalformedURLException。必须定义一个显式构造函数。我认为这是因为如果我的 url 无效,则没有对应catch的错误可以捕获(因为我在 之外声明了我的 url try)。

But, i want to define this url outside of try, because my main tryblock is in a loop and the url is static. So there's no need to do the definition of the url more than once (and because it's static i'm not afraid of any MalformedURLExceptions).

但是,我想在 之外定义这个 url try,因为我的主try块在一个循环中并且 url 是静态的。所以没有必要多次定义 url(并且因为它是静态的,我不怕任何 MalformedURLExceptions)。

How do I do that? Is there really no other way than to define the url within a separate tryblock? (because that seems a bit too much for just a simple static url)

我怎么做?除了在单独的try块中定义 url 之外,真的没有其他方法吗?(因为对于一个简单的静态 url 来说这似乎有点太多了)



// import libraries
import java.net.*;
import java.io.*;

// url
URL base = new URL("http://www.google.com");

void setup() {
}

void draw() {
  try {
    // retrieve url
    String[] results = loadStrings(base);

    // print results
    println(results[0]);
  }  
  catch (MalformedURLException e) {
    e.printStackTrace();
  }
  catch (ConnectException e) {
    e.printStackTrace();
  }
  catch (IOException e) {
    e.printStackTrace();
  }

  // stop looping
  noLoop();

}

回答by Collin

You just need to define a default constructor for your class which throws MalformedURLException:

你只需要为你的类定义一个默认构造函数,它会抛出 MalformedURLException:

class MyClass {
    private URL url = new URL("http://www.google.com");

    public MyClass() throws MalformedURLException {}
}

The error is happening because the constructor Java is generating for your class isn't able to throw anything, you must specify that it might.

发生错误是因为 Java 为您的类生成的构造函数无法抛出任何内容,您必须指定它可能会抛出。

Edit

编辑

Seeing your code, your other option is this (I assume setup() gets called before you use the URL)

看到你的代码,你的另一个选择是这个(我假设 setup() 在你使用 URL 之前被调用)

URL url;

void setup() {
    try {
        url = new URL("http://www.google.com");
    } catch (MalformedURLException ex) {
        throw new RuntimeException(ex);
    }
}

回答by Jord?o

Since the URLconstructor throws a checked exception you have to either catch it or rethrow it. You can create your own factory method that just wraps it in a RuntimeException:

由于URL构造函数抛出已检查的异常,因此您必须捕获它或重新抛出它。您可以创建自己的工厂方法,将其包装在一个RuntimeException

public class URLFactory {
  public static URL create(String urlString) {
    try {
      return new URL(urlString);
    } catch (MalformedURLException e) {
      throw new RuntimeException(e);
    }
  }
}

Or maybe just this in your environment?

或者也许只是在您的环境中如此?

URL createURL(String urlString) {
  try {
    return new URL(urlString);
  } catch (MalformedURLException e) {
    throw new RuntimeException(e);
  }
}

And use it instead:

并改用它:

URL base = URLFactory.create("http://www.google.com");