问题描述
我已阅读: - 这给了我正确的想法,并带领我一个答案,但不是很明确。我也读了几个人,但找不到我的答案。要重写我所学习的上下文,基本上不会编译...
public ExampleClass(String FileName)
{
this(new FileInputStream(FileName));
}
public ExampleClass(FileInputStream FileStream)
{
DoSomethingToSetupBased UponFileStream(FileStream);
}
...因为 FileInputStream
构造函数(从String构造函数调用)可能会抛出一个FileNotFoundException。您仍然可以通过使它引发相同的异常来创建构造函数,如下所示:
public ExampleClass String FileName)throws FileNotFoundException
{
this(new FileInputStream(FileName));
}
我的问题与一个默认构造函数(无参数)有关,默认文件名字符串常量:
public ExampleClass()throws FileNotFoundException
{
this(DEFAULT_FILE_NAME);
}
这将把构造函数链接为:
ExampleClass()
- > ExampleClass(< String>)
- > ExampleClass(< InputFileStream>)
在这种情况下,是否可以使用默认值(静态最终类成员)在默认构造函数中,实例化(进一步向下链)FileInputStream,但不必使用 throws FileNotFoundException
代码(这将需要某人使用该类可以重新抛出或处理异常?
如果我可以执行以下操作,我将自己处理异常:
public ExampleClass()
pre>
{
try
{
this(DEFAULT_FILE_NAME);
}
catch(异常e)
{
DoSomethingToHandleException(e);
}
}
...但是据我所知,这是不可能的,因为构造函数调用必须是构造函数中的第一个语句
在这一点上更习惯使用.Net,我从来没有被强制 / strong>来处理异常,如果我真的不想...:D
解决方案重构你的文件构造代码在你的构造函数中,所以你可以这样做 -
public ExampleClass(){
/ pre>
try {
fileInputStreamMethod(DEFAULT_FILE);
}
catch(异常e){
...
}
public ExampleClass(String fileName)throws异常{
fileInputStreamMethod文件名);
}
private void fileInputStreamMethod(String fileName)throws异常{
//您的文件处理方法
}
I've read this: Can I use throws in constructor? -- which gave me the right idea, and led me to one answer, but was not very explicit. I've also read several others, but could not find my answer. To recap what I've learned for context, essentially, this will not compile...
public ExampleClass(String FileName) { this(new FileInputStream(FileName)); } public ExampleClass(FileInputStream FileStream) { DoSomethingToSetupBasedUponFileStream(FileStream); }
...because the
FileInputStream
constructor (called from the String Constructor) may throw a FileNotFoundException. You can still create the constructor by making it throw the same exception as follows:public ExampleClass(String FileName) throws FileNotFoundException { this(new FileInputStream(FileName)); }
My question is related to a default constructor (no arguments) that would simply use a default filename String constant:
public ExampleClass() throws FileNotFoundException { this(DEFAULT_FILE_NAME); }
This would chain the constructors as:
ExampleClass()
-->ExampleClass(<String>)
-->ExampleClass(<InputFileStream>)
In a case like this, is it possible to use a default value (static final class member) in the default constructor, to instantiate (further down the chain) a FileInputStream, but not have to use the
throws FileNotFoundException
code (which would require someone using the class to either re-throw or handle the exception?If I could do something like the following, I would handle the exception myself:
public ExampleClass() { try { this(DEFAULT_FILE_NAME); } catch (Exception e) { DoSomethingToHandleException(e); } }
...However, as far as I know this is not possible, because the "Constructor call must be the first statement in a constructor"
Being more used to .Net at this point, I've never been forced to deal with exceptions if I didn't really want to... :D
解决方案Refactor your file construction code out of your constructor, so you could do something like this --
public ExampleClass() { try { fileInputStreamMethod(DEFAULT_FILE); } catch(Exception e) { ... } public ExampleClass(String fileName) throws Exception { fileInputStreamMethod(fileName); } private void fileInputStreamMethod(String fileName) throws Exception { // your file handling methods }
这篇关于在Java中链接构造函数,而不会从默认构造函数中抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!