This is a common problem when running Java:
Error message: default constructor cannot handle exception type FileNotFoundException throw by implicit super constructor. Must define an explicit constructor.
The default constructor cannot handle the exception type FileNotFoundException thrown by the implicit super constructor. You must define an explicit constructor.
Specific situation: when testing the usage method of RandomAccessFile class
A RandomAccessFile type [global variable] is declared.
import java.io.FileNotFoundException;
import java.io.RandomAccessFile;
public class MyRandomAccessFile {
private RandomAccessFile file = new RandomAccessFile("D:/demo/test.txt", "rw");
}
Then I searched and farted.
I checked the API of the constructor of RandomAccessFile:
public RandomAccessFile(String name,String mode)throws FileNotFoundException
FileNotFoundException - if the mode is "r" but the given string does not denote an existing regular file,
or if the mode begins with "rw" but the given string does not denote an existing, writable regular file and a new regular file of that name cannot be created,
or if some other error occurs while opening or creating the file
I first checked the method of converting the implicit constructor to the display constructor, and then checked the details of this error. Mu you.
Finally, when I saw the API, I realized that was declared in the constructor of this method
In this way, we cannot directly use this method because the class name cannot declare throws, so:
1. Create a new method, take this RandomAccessFile as the return value type, and declare throws.
public RandomAccessFile createRAFByfilename(String filename,String mode)throws FileNotFoundException{
return new RandomAccessFile(filename, mode);
}
2. Declare throws directly on the method to create the RandomAccessFile object.
public static void main() throws FileNotFoundException {
MyRandomAccessFile mraf = new MyRandomAccessFile();
RandomAccessFile file = mraf.createRAFByfilename("D:/demo/test.txt", "rw");
}
In short, it has nothing to do with implicit and explicit constructors.
Read More:
- Net Q & A: how to avoid the exception thrown by max() on emptyenumerable?
- Solve the unexpected end of stream exception thrown by jedis
- Nodejs: TypeError: The super constructor to “inherits“ must not be null or undefined
- Kotlin reported an error jsonexception using fastjason: default constructor not found
- How to handle exception in springboot
- Failed to instantiate org.mybatis.spring.SqlSessionTemplate Constructor threw exception
- A stopiteration exception is thrown after Python next() completes
- mongo Failed to instantiate *** constructor NO_CONSTRUCTOR with arguments
- Vue project error: uncaught typeerror: vuex__ WEBPACK_ IMPORTED_ MODULE_ 1__ . default.store is not a constructor
- Module parse failed:Unexpectedtoken (1:0)You may need an appropriate loader to handle this file type
- You may need an appropriate loader to handle this file type, currently no loaders are configured to
- Python Anaconda Spyder can’t display pictures by using Matplotlib. Error report solution: figures now render in the plots pane by default. To mak
- error: Type of the default value for ‘tableData‘ prop must be a function
- [Warning] incompatible implicit declaration of built-in function ‘strcat’
- Solve the problem of using the default content type of JMeter
- nvalid default value for prop “params“: Props with type Object/Array must use a factory function to
- The browser requires that samesite by default cookies be disabled
- Pit encountered by entity class data type BigDecimal
- ImageIO.read () unsupported image type when reading picture_ exception Unsupported Image Type
- Run with Python console solution is set by default for all projects in pcharm