I have the following code where I need to catch AccessDeniedException exception
import java.io.PrintWriter;
import java.io.IOException;
import java.nio.file.AccessDeniedException;
class MyFileClass {
public void write()
throws IOException
{
PrintWriter out = new PrintWriter("sample.txt");
out.printf("%8.2f\n", 3.4);
out.close();
}
}
public class MyClass {
public static void main(String[] args)
throws Exception
{
try {
MyFileClass mf = new MyFileClass();
mf.write();
} catch (AccessDeniedException e) {
print("Access denided");
}
catch (FileNotFoundException e) {
print("File not found");
}
}
}
In case sample.txt is read only, I get output as "file not found" rather "Access denided". I would like to understand what is the reason for this? Also, is the above structure for catching AccessDeniedException correct?
AccessDeniedException is only thrown by the new file API; the old file API (which you use with this PrintWriter constructor) only knows how to throw FileNotFoundException even if the real filesystem-level problem is not "the file does not exist".
You have to use the new API to open an output stream to the destination file; then you can have meaningful exceptions:
// _will_ throw AccessDeniedException on access problems
final OutputStream out = Files.newOutputStream(Paths.get(filename));
final PrintWriter writer = new PrintWriter(out);
More generally, the new file API defines FileSystemException (inheriting IOException), which all new, meaningful exceptions defined by the new API inherit.
This means among other things that you can clearly separate, in catch clauses, what is caused by filesystem-level errors and "real" I/O errors, which you can't do with the old API:
try {
// some new file API operation
} catch (FileSystemException e) {
// deal with fs error
} catch (IOException e) {
// deal with I/O error
}
There is NO such AccessDeniedException in PrintWriter.
SecurityException is the exception thrown by PrintWriter
If a security manager is present and checkWrite(file.getPath()) denies
write access to the file
Related
My compiler is yelling at me because of this function:
public static void writeKills() {
FileOutputStream f = new FileOutputStream(new File("plugins/core/killstats.db"));
ObjectOutputStream o = new ObjectOutputStream(f);
sKillStats.keySet().forEach(user -> o.writeObject(sKillStats.get(user)));
}
ERROR:
[ERROR] /C:/Users/lyser/git/RVAS-Core/rvas-core_v0.1.1/src/main/java/core/backend/PlayerMeta.java:[325,66] unreported exception java.io.IOException; must be caught or declared to be thrown
[ERROR] /C:/Users/lyser/git/RVAS-Core/rvas-core_v0.1.1/src/main/java/core/backend/PlayerMeta.java:[322,38] unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
[ERROR] /C:/Users/lyser/git/RVAS-Core/rvas-core_v0.1.1/src/main/java/core/backend/PlayerMeta.java:[323,32] unreported exception java.io.IOException; must be caught or declared to be thrown```
I tried throwing and catching the exception like this to no avail:
public static void writeKills() throws IOException {
FileOutputStream f = new FileOutputStream(new File("plugins/core/killstats.db"));
ObjectOutputStream o = new ObjectOutputStream(f);
try {
sKillStats.keySet().forEach(user -> o.writeObject(sKillStats.get(user)));
} catch (IOException e) {
e.printStackTrace();
}
}
Try-catching the entire method produces the same error.
I have no idea where to catch please help.
sKillStats.keySet().forEach(user -> o.writeObject(sKillStats.get(user)));
o.writeObject throws IOException, a checked exception. You need to handle that exception inside the forEach Consumer, because Consumers can't throw checked exceptions.
Either catch it inside the forEach:
.forEach(user -> {
try {
o.writeObject(sKillStats.get(user));
} catch (IOException e) {
throw new UncheckedIOException(e); // Or whatever.
}
})
or, better, write it as an enhanced for loop:
for (var user : sKillStats.keySet()) {
o.writeObject(sKillStats.get(user));
}
and then the IOException is handled by the throws IOException on the method.
Note that you can also just iterate the values() of the map, rather than iterating the keys and getting the value:
for (var value : sKillStats.values()) {
o.writeObject(value);
}
Note as well that you need to close your streams. The easiest way to do this is with try-with-resources:
try (FileOutputStream f = new FileOutputStream(new File("plugins/core/killstats.db"));
ObjectOutputStream o = new ObjectOutputStream(f)) {
// ...
}
Ive two method that throw the same exception throws IOException
the problem is that each method throw it from different reason, I wrap the methods
in the main with try catch, what is the recommended way to solve it ?
I need different message for each exception with the same type..
public static void main(String[] args) {
try{
….
readFile(path);
convert(file)
} catch (IOException e) {
…..
}
private static String readFile(String path) throws IOException {
//Here files requires IOException - Reason here cannot Read file
String lineSeparator = System.getProperty("line.separator");
List<String> lines = Files.readAllLines(Paths.get(path));
}
private static String convert(String file) throws IOException {
//Here reader requires also ioException- Reason here cannot parse file
ObjectMapper reader = new ObjectMapper(new YAMLFactory());
Object obj = reader.readValue(file, Object.class);
}
There are several ways you could approach this. One way, perhaps the heaviest in terms of new code you would need to write, would be throw a custom exception from each of your helper methods. Then you could catch each specific exception in a separate block.
But what I might recommend here is that you simply wrap each of the two calls to your helper methods in separate try-catch blocks:
try {
readFile(path);
} catch (IOException e) {
// handle first exception here
}
// more code
try {
convert(file)
} catch (IOException e) {
// handle second exception here
}
This is fairly clean and doesn't require a lot of refactoring. If you keep encountering this problem, then maybe consider creating custom exceptions for your application. If you have a look at many Java libraries, you will see that they often use their own custom exceptions.
If you wanted to go the route of using a custom exception, you could define one, e.g.
public class FileReadIOException extends Exception {
public FileReadIOException(String message) {
super(message);
}
}
and then use it:
private static String readFile(String path) throws FileReadIOException {
try {
String lineSeparator = System.getProperty("line.separator");
List<String> lines = Files.readAllLines(Paths.get(path));
}
catch (Exception e) {
throw new FileReadIOException(e.getMessage());
}
}
try {
readFile(path);
// more code
convert(file)
} catch (FileReadIOException e) {
// handle first exception here
} catch (SomeOtherException e) {
// handle second exception here
}
The above code showing custom exception is a bit contrived, because the reality is that all of your code is throwing IOException. Creating custom exceptions in your case does not add much value, because they are already (rightfully) throwing an IOException. I'm not sure that it doesn't make sense to handle just one type of exception. More typically, if you were working on a large enterprise application, you would use custom exceptions to handle situations which go wrong in your own custom code.
My way is usually to create your own exception and throw it like that
public class Snippet {
public static void main(String[] args) {
try {
String path = "";
readFile(path);
String file = "";
convert(file);
} catch (MyException e) {
// do whatever
}
}
private static String readFile(String path) throws MyException {
try {
String lineSeparator = System.getProperty("line.separator");
List<String> lines = Files.readAllLines(Paths.get(path));
} catch (Exception e) {
throw new MyException("Custom 'readFile' message", e);
}
}
private static String convert(String file) throws MyException {
try {
ObjectMapper reader = new ObjectMapper(new YAMLFactory());
Object obj = reader.readValue(file, Object.class);
} catch (Exception e) {
throw new MyException("Custom 'convert' message", e);
}
}
}
class MyException extends Exception {
private static final long serialVersionUID = -3166824380774329449L;
public MyException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
}
Tim's way is valid too.
#OP The best person to solve this would be you yourself.
There are quite easily multiple ways to solve that. Check which one suits you more properly.
One of the solution as per me is below. This is based on the reason IOException is superclass to many other exceptions. IOException documentation
try {
readFile(path);
} catch ( FileNotFoundException e) {
// handle first exception here
} catch ( EOFException e) {
// handle 2nd exception here
}
For the above to work you would need to know which type of IOException is being thrown.
Another solution is check individual exception messages when you know what are expected messages you may receive.
try {
readFile(path);
} catch ( IOException e) {
if(e. getMessage().contains("TXT 1...") {
//Case handle #1
} else if(e. getMessage().contains("TXT 2...") {
//Case handle #2
}
}
The best way to handle exception is not to have them. When exception is thrown, it indicates that natural lifecycle of your application has been interrupted for one reason or another. Most exceptions are self-explanatory and provide you with precise explanation of what happend, therefore creating new exceptions and re-mapping those thrown is almost always counterproductive and may result in more confusion than usefulness(especial if you work in a team).
Furthermore exceptions doesn't need to be terminal, in most cases, a scenario can be devised to retry/prompt different input etc. to ensure that lifecycle is not interrupted. Killing an application on exception can create more problems in some cases (e.g: not closing files properly, hence losing processed data).
Now to your actual problem. If you have two or more components, that throws unrelated exceptions(name is not a relation in this case), it is best not to have them in the same try/catch structure as less workaround will be necesary to scrap that one part, instead of the whole thing which can still exit on its own, or doesn't even need to be initiated.
Here is my 5 cents
public static void main(String[] args) throws Exception {
String path = "path";
String path2 = "path2";
try{
readFile(path);
} catch (IOException e) {
throw new Exception("read file exception", e);
}
try{
convert(path2);
} catch (IOException e) {
throw new Exception("convert exception", e);
}
}
private static String readFile(String path) throws IOException {
//Here files requires IOException - Reason here cannot Read file
String lineSeparator = System.getProperty("line.separator");
List<String> lines = Files.readAllLines(Paths.get(path));
}
private static String convert(String file) throws IOException {
//Here reader requires also ioException- Reason here cannot parse file
ObjectMapper reader = new ObjectMapper(new YAMLFactory());
Object obj = reader.readValue(file, Object.class);
}
Could you help me understand where should I throw exceptions and catch them.
Please, have a look at my code. I thought that in Thrd class I have already thrown and caught the exception. But when I wrote in the main class FirstThread.readFile("ParallelProgramming.txt");, I faced a runtime error - unhandled exception. So, I had to use try and catch. So, I somehow can't understand why in the Thd class my try and catch blocks didn't work.
package parallelprogramming;
import java.lang.Thread;
import java.io.*;
public class Thrd extends Thread {
public void readFile(String File) throws FileNotFoundException {
FileReader fr = new FileReader(File);
BufferedReader br = new BufferedReader(fr);
String s;
try {
while ((s = br.readLine()) != null) {
System.out.println(s);
}
fr.close();
}
catch (FileNotFoundException FNFD) {
System.out.println("File not found!");
}
catch (IOException IOE){
System.out.println("IOException caught!");
}
}
}
package parallelprogramming;
import java.io.FileNotFoundException;
public class ParallelProgramming {
public static void main(String[] args) throws FileNotFoundException {
Thrd FirstThread = new Thrd();
try {
FirstThread.readFile("ParallelProgramming.txt");
} catch (FileNotFoundException FNFD) {
System.out.println("File not found!");
}
}
}
The rules with checked exceptions (and this includes IOException, which FileNotFoundException is a child of), are as follows:
if you cannot, or do not want, to handle it in your current method, declare that the method throws it;
if you want to handle it in your current method, then catch it; note that even in this case you can rethrow that exception;
if main() throws any exception, and this exception triggers, the program terminates.
Now, we suppose that you are using Java 7. In this case, do that:
public void readFile(final String file)
throws IOException
{
final Path path = Paths.get(file);
for (final String line: Files.readAllLines(path, StandardCharsets.UTF_8))
System.out.println(line);
}
Why bother? ;)
If you don't want to do that but read line by line, then:
public void readFile(final String file)
throws IOException
{
final Path path = Paths.get(file);
try (
final BufferedReader reader = Files.newBufferedReader(path,
StandardCharsets.UTF_8);
) {
String line;
while ((line = reader.readLine()) != null)
System.out.println(line);
}
}
The second form is preferred if you wish to treat exceptions specifically. Note that with Java 7, you have meaningful exceptions as to why you cannot access the file vs IOException: NoSuchFileException, AccessDeniedException, etc etc. All these inherit FileSystemException. The old file API can't do that for you.
This means that if you want to deal with filesystem level errors you can do:
catch (FileSystemException e) { /* ... */ }
where before that you did:
catch (FileNotFoundException e) { /* ... */ }
Translated to the code above, if you want to catch exceptions you'll then do:
// All exceptions handled within the method -- except if you rethrow it
public void readFile(final String file)
{
final Path path = Paths.get(file);
try (
final BufferedReader reader = Files.newBufferedReader(path,
StandardCharsets.UTF_8);
) {
String line;
while ((line = reader.readLine()) != null)
System.out.println(line);
} catch (FileSystemException e) {
// deal with a filesystem-level error
// Note that you MUSt catch it before IOException
// since FileSystemException inherits IOException
} catch (IOException e) {
// deal with a low-level I/O error
}
}
Remove 'throws FileNotFoundException' from the readFile method of class Thrd then you dont need to handle this exception in main method of class ParallelProgramming.
As you throw FileNotFoundException from readFile method then exception will pass to the method will called this i.e. then main method need to handle this exception.
There are two ways to handle an exception either you need to catch it or throw it again.
In the readFile method you have done both. you caught the exceptions using catch blocks then you have mentioned that readFile method throws FileNotFoundException, so when ever you use readFile method you need to catch the exception again.
I am total noob at Android dev. I am still learning, and I am on very first step of my app dev.
I have this code working and it runs fine or regular Java, now I am trying to implement to Android OS.
In my code where it says TEST.openStream() I get Unhandled exception type IOException error.
package com.zv.android;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.os.Bundle;
public class ZipActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
URL TEST = new URL("http://www.google.com/");
BufferedReader in = new BufferedReader(new InputStreamReader(TEST.openStream()));
String inputLine;
int line=0;
while ((inputLine = in.readLine()) != null){
line++;
//System.out.println(line + "\t" + inputLine);
}
// in.close();
} catch(MalformedURLException e) {
//Do something with the exception.
}
}
}
Error message is simple: you need to catch the IOException, this because URL.openStream() is declared as
public final InputStream openStream() throws IOException
so by accepting the contract of the method you also accept the fact that you must handle this exception, this is how it works in Java. This is a checked exceptions, then it must be caught because this kind of exceptions represent situations that may arise and that your code must handle.
To catch it just add another case in your try statement:
try {
..
catch (MalformedURLException e) {
..
}
catch (IOException e) {
..
}
Just as a final note: you don't need to catch it when you call the openStream() method, you could state that the method that calls openStream() will forward the exception to the caller but in the end of the call chain you will have to catch it in any case.
Catch IOException also, like how you did for MalformedURLException (or) declare method as throws IOException.
try {
URL TEST = new URL("http://www.google.com/");
BufferedReader in = new BufferedReader(new InputStreamReader(TEST.openStream()));
String inputLine;
int line=0;
while ((inputLine = in.readLine()) != null){
line++;
//System.out.println(line + "\t" + inputLine);
}
// in.close();
} catch(MalformedURLException e) {
//Do something with the exception.
} catch(IOException e2) {
//Do something with the exception.
}
IOException is checked exception, which either need to be catch (or) re-throw. See this tutorial for more information.
You need to catch IOException because openStream() method may throw IOException in abnormal case.
TEST.openStream()
might throw an IOException which is a checked exception in java, thus you have to either Handle the IOException using try/catch blocks or declare IOException in your method signature using throws clause.
you have to handle IOException in your catch block .
try {
URL TEST = new URL("http://www.google.com/");
BufferedReader in = new BufferedReader(new InputStreamReader(TEST.openStream()));
//rest of your code
} catch(MalformedURLException e) {
//Do something with the exception.
}
catch(MalformedURLException e) {
//Do something with the exception.
}
catch(IOException ex) {
ex.printStackTrace();
}
Declaring IOException in your method signature:
public void onCreate(Bundle savedInstanceState) throws IOException {
in this case you dont wrap your code inside a try/catch, although i would strongly recommend always handle the exception using try/catch rather than declaring it using throws clause.
I have code that looks something like:
public static void func(Reader r){
int c = r.read();
...
}
and the compiler tells me that r.read() might throw an IOException. Under what circumstances might this happen? It seems clear that something like FileNotFoundException is thrown when a file is not found, but an IOException is rather vague.
Edit:
In case it is of interest to anyone, I asked this question because I thought there must be a better way of handling the situation than just printStackTrace. However, without knowing what might cause the exception, I wasn't really sure how it should be done.
Lots of things can cause an IOException. When it gets thrown, you can print it out or check the message (Exception.getMessage()) to see what caused it.
A FileNotFoundException is a subclass of IOException, you can check the "known subclasses" list for others.
For example:
public void load(InputStream inputStream) throws IOException {
this.inputStream = inputStream;
this.properties.load(this.inputStream);
this.keys = this.properties.propertyNames();
inputStream.close();
}
I think that's when you have problem with input / output (connections) because of security or for example not opening the stream.
Code source: stackoverflow
It can throw an IOException when the either the stream itself is corrupted or some error occurred during reading the data i.e. Security Exceptions, Permission Denied etc and/or a set of Exceptions which are derived from IOEXception.
IOException is superclass of many exceptions like CharConversionException, CharacterCodingException and EOFException.
If the method listed all these exceptions then caller would have to catch all of them. So for convenience having IOException in throws clause helps callers to avoid multiple catch blocks. User can still handle specific exception if they desire so by checking for instance of or exception.getMessage().
If you want to know the specifics, in your catch block do this:
catch (IOException e)
{
e.printStackTrace();
}
This code will help you to debug and to see the IOException thrown:
String NL = System.getProperty("line.separator");
String line;
FileInputStream in;
try {
fileName = choose.getSelectedFile().getCanonicalPath();
} catch (IOException e) {
e.printStackTrace(); //This doesn't matter, see the second IOException catch.
}
try {
in = new FileInputStream(choose.getSelectedFile());
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
StringBuffer content=new StringBuffer("");
while((line = reader.readLine()) != null){
content.append(line+NL);
}
textArea.setText(content.toString());
reader.close();
reader.readLine();
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(new JFrame(), "The file does not exist!", "Error", JOptionPane.WARNING_MESSAGE);
} catch (IOException e) {
JOptionPane.showMessageDialog(new JFrame(), "There was an error in reading the file.", "Error", JOptionPane.WARNING_MESSAGE);
}
Good luck.