Wrap it around try/catch block
as File#createNewFile() might throw IOException in case of IOError.IOException is a checked exception and in java compiler will force you to handle/declare
checked exceptions in the code yourself.
try {
File f= new File("Buns.dat");
f.createNewFile();
}
catch(IOException ex){
ex.printStacktrace();
}
From java 1.7 using try-with-resource Statement:
try(File f= new File("Buns.dat")) {
f.createNewFile();
}
catch(IOException ex){
ex.printStacktrace();
}
If you choose to use try-with-resource statement, the only difference is that you don't need to closeyour resouces explicitly using finally block. To use try-with-resource though the object which you use inside the try-with-resource statement must implement
java.lang.AutoCloseable`.
You can also propagate the exception by using throws clause
in your method signature.
public static void main(String[] args) throws IOException {
Related: