Why do I get the “Unhandled exception type IOException”?
I have the following simple code:
import java.io.*;
class IO {
public static void main(String args) {
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;
while ((userInput = stdIn.readLine()) != null) {
System.out.println(userInput);
}
}
}
And I get the following error message:
----------
1. ERROR in io.java (at line 10)
while ((userInput = stdIn.readLine()) != null) {
^^^^^^^^^^^^^^^^
Unhandled exception type IOException
----------
1 problem (1 error)roman@roman-laptop:~/work/java$ mcedit io.java
Does anybody have any ideas why? I just tried to simplify the code given on the sum web site (here). Did I oversimplify?
java stdin readline ioexception
add a comment |
I have the following simple code:
import java.io.*;
class IO {
public static void main(String args) {
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;
while ((userInput = stdIn.readLine()) != null) {
System.out.println(userInput);
}
}
}
And I get the following error message:
----------
1. ERROR in io.java (at line 10)
while ((userInput = stdIn.readLine()) != null) {
^^^^^^^^^^^^^^^^
Unhandled exception type IOException
----------
1 problem (1 error)roman@roman-laptop:~/work/java$ mcedit io.java
Does anybody have any ideas why? I just tried to simplify the code given on the sum web site (here). Did I oversimplify?
java stdin readline ioexception
add a comment |
I have the following simple code:
import java.io.*;
class IO {
public static void main(String args) {
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;
while ((userInput = stdIn.readLine()) != null) {
System.out.println(userInput);
}
}
}
And I get the following error message:
----------
1. ERROR in io.java (at line 10)
while ((userInput = stdIn.readLine()) != null) {
^^^^^^^^^^^^^^^^
Unhandled exception type IOException
----------
1 problem (1 error)roman@roman-laptop:~/work/java$ mcedit io.java
Does anybody have any ideas why? I just tried to simplify the code given on the sum web site (here). Did I oversimplify?
java stdin readline ioexception
I have the following simple code:
import java.io.*;
class IO {
public static void main(String args) {
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;
while ((userInput = stdIn.readLine()) != null) {
System.out.println(userInput);
}
}
}
And I get the following error message:
----------
1. ERROR in io.java (at line 10)
while ((userInput = stdIn.readLine()) != null) {
^^^^^^^^^^^^^^^^
Unhandled exception type IOException
----------
1 problem (1 error)roman@roman-laptop:~/work/java$ mcedit io.java
Does anybody have any ideas why? I just tried to simplify the code given on the sum web site (here). Did I oversimplify?
java stdin readline ioexception
java stdin readline ioexception
asked Feb 21 '10 at 13:14
RomanRoman
27.6k124277366
27.6k124277366
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
You should add "throws IOException" to your main method:
public static void main(String args) throws IOException {
You can read a bit more about checked exceptions (which are specific to Java) in JLS.
add a comment |
Java has a feature called "checked exceptions". That means that there are certain kinds of exceptions, namely those that subclass Exception but not RuntimeException, such that if a method may throw them, it must list them in its throws declaration, say: void readData() throws IOException. IOException is one of those.
Thus, when you are calling a method that lists IOException in its throws declaration, you must either list it in your own throws declaration or catch it.
The rationale for the presence of checked exceptions is that for some kinds of exceptions, you must not ignore the fact that they may happen, because their happening is quite a regular situation, not a program error. So, the compiler helps you not to forget about the possibility of such an exception being raised and requires you to handle it in some way.
However, not all checked exception classes in Java standard library fit under this rationale, but that's a totally different topic.
add a comment |
Try again with this code snippet:
import java.io.*;
class IO {
public static void main(String args) {
try {
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;
while ((userInput = stdIn.readLine()) != null) {
System.out.println(userInput);
}
} catch(IOException ie) {
ie.printStackTrace();
}
}
}
Using try-catch-finally
is better than using throws
. Finding errors and debugging are easier when you use try-catch-finally
.
add a comment |
Reading input from keyboard is analogous to downloading files from the internet, the java io system opens connections with the source of data to be read using InputStream or Reader, you have to handle a situation where the connection can break by using IOExceptions
If you want to know exactly what it means to work with InputStreams and BufferedReader this video shows it
add a comment |
add "throws IOException" to your method like this:
public static void main(String args) throws IOException{
FileReader reader=new FileReader("db.properties");
Properties p=new Properties();
p.load(reader);
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2305966%2fwhy-do-i-get-the-unhandled-exception-type-ioexception%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
You should add "throws IOException" to your main method:
public static void main(String args) throws IOException {
You can read a bit more about checked exceptions (which are specific to Java) in JLS.
add a comment |
You should add "throws IOException" to your main method:
public static void main(String args) throws IOException {
You can read a bit more about checked exceptions (which are specific to Java) in JLS.
add a comment |
You should add "throws IOException" to your main method:
public static void main(String args) throws IOException {
You can read a bit more about checked exceptions (which are specific to Java) in JLS.
You should add "throws IOException" to your main method:
public static void main(String args) throws IOException {
You can read a bit more about checked exceptions (which are specific to Java) in JLS.
edited May 29 '17 at 4:02
Muhd
10.7k175369
10.7k175369
answered Feb 21 '10 at 13:18
MarcinMarcin
5,85653848
5,85653848
add a comment |
add a comment |
Java has a feature called "checked exceptions". That means that there are certain kinds of exceptions, namely those that subclass Exception but not RuntimeException, such that if a method may throw them, it must list them in its throws declaration, say: void readData() throws IOException. IOException is one of those.
Thus, when you are calling a method that lists IOException in its throws declaration, you must either list it in your own throws declaration or catch it.
The rationale for the presence of checked exceptions is that for some kinds of exceptions, you must not ignore the fact that they may happen, because their happening is quite a regular situation, not a program error. So, the compiler helps you not to forget about the possibility of such an exception being raised and requires you to handle it in some way.
However, not all checked exception classes in Java standard library fit under this rationale, but that's a totally different topic.
add a comment |
Java has a feature called "checked exceptions". That means that there are certain kinds of exceptions, namely those that subclass Exception but not RuntimeException, such that if a method may throw them, it must list them in its throws declaration, say: void readData() throws IOException. IOException is one of those.
Thus, when you are calling a method that lists IOException in its throws declaration, you must either list it in your own throws declaration or catch it.
The rationale for the presence of checked exceptions is that for some kinds of exceptions, you must not ignore the fact that they may happen, because their happening is quite a regular situation, not a program error. So, the compiler helps you not to forget about the possibility of such an exception being raised and requires you to handle it in some way.
However, not all checked exception classes in Java standard library fit under this rationale, but that's a totally different topic.
add a comment |
Java has a feature called "checked exceptions". That means that there are certain kinds of exceptions, namely those that subclass Exception but not RuntimeException, such that if a method may throw them, it must list them in its throws declaration, say: void readData() throws IOException. IOException is one of those.
Thus, when you are calling a method that lists IOException in its throws declaration, you must either list it in your own throws declaration or catch it.
The rationale for the presence of checked exceptions is that for some kinds of exceptions, you must not ignore the fact that they may happen, because their happening is quite a regular situation, not a program error. So, the compiler helps you not to forget about the possibility of such an exception being raised and requires you to handle it in some way.
However, not all checked exception classes in Java standard library fit under this rationale, but that's a totally different topic.
Java has a feature called "checked exceptions". That means that there are certain kinds of exceptions, namely those that subclass Exception but not RuntimeException, such that if a method may throw them, it must list them in its throws declaration, say: void readData() throws IOException. IOException is one of those.
Thus, when you are calling a method that lists IOException in its throws declaration, you must either list it in your own throws declaration or catch it.
The rationale for the presence of checked exceptions is that for some kinds of exceptions, you must not ignore the fact that they may happen, because their happening is quite a regular situation, not a program error. So, the compiler helps you not to forget about the possibility of such an exception being raised and requires you to handle it in some way.
However, not all checked exception classes in Java standard library fit under this rationale, but that's a totally different topic.
answered Feb 21 '10 at 13:23
jkffjkff
12.9k23370
12.9k23370
add a comment |
add a comment |
Try again with this code snippet:
import java.io.*;
class IO {
public static void main(String args) {
try {
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;
while ((userInput = stdIn.readLine()) != null) {
System.out.println(userInput);
}
} catch(IOException ie) {
ie.printStackTrace();
}
}
}
Using try-catch-finally
is better than using throws
. Finding errors and debugging are easier when you use try-catch-finally
.
add a comment |
Try again with this code snippet:
import java.io.*;
class IO {
public static void main(String args) {
try {
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;
while ((userInput = stdIn.readLine()) != null) {
System.out.println(userInput);
}
} catch(IOException ie) {
ie.printStackTrace();
}
}
}
Using try-catch-finally
is better than using throws
. Finding errors and debugging are easier when you use try-catch-finally
.
add a comment |
Try again with this code snippet:
import java.io.*;
class IO {
public static void main(String args) {
try {
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;
while ((userInput = stdIn.readLine()) != null) {
System.out.println(userInput);
}
} catch(IOException ie) {
ie.printStackTrace();
}
}
}
Using try-catch-finally
is better than using throws
. Finding errors and debugging are easier when you use try-catch-finally
.
Try again with this code snippet:
import java.io.*;
class IO {
public static void main(String args) {
try {
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;
while ((userInput = stdIn.readLine()) != null) {
System.out.println(userInput);
}
} catch(IOException ie) {
ie.printStackTrace();
}
}
}
Using try-catch-finally
is better than using throws
. Finding errors and debugging are easier when you use try-catch-finally
.
edited Oct 23 '15 at 14:17
Younes Regaieg
3,66221334
3,66221334
answered Feb 21 '10 at 15:01
ParthParth
3711617
3711617
add a comment |
add a comment |
Reading input from keyboard is analogous to downloading files from the internet, the java io system opens connections with the source of data to be read using InputStream or Reader, you have to handle a situation where the connection can break by using IOExceptions
If you want to know exactly what it means to work with InputStreams and BufferedReader this video shows it
add a comment |
Reading input from keyboard is analogous to downloading files from the internet, the java io system opens connections with the source of data to be read using InputStream or Reader, you have to handle a situation where the connection can break by using IOExceptions
If you want to know exactly what it means to work with InputStreams and BufferedReader this video shows it
add a comment |
Reading input from keyboard is analogous to downloading files from the internet, the java io system opens connections with the source of data to be read using InputStream or Reader, you have to handle a situation where the connection can break by using IOExceptions
If you want to know exactly what it means to work with InputStreams and BufferedReader this video shows it
Reading input from keyboard is analogous to downloading files from the internet, the java io system opens connections with the source of data to be read using InputStream or Reader, you have to handle a situation where the connection can break by using IOExceptions
If you want to know exactly what it means to work with InputStreams and BufferedReader this video shows it
edited Jan 21 '13 at 7:44
Veger
29.4k890104
29.4k890104
answered Jan 21 '13 at 1:27
vivzvivz
171
171
add a comment |
add a comment |
add "throws IOException" to your method like this:
public static void main(String args) throws IOException{
FileReader reader=new FileReader("db.properties");
Properties p=new Properties();
p.load(reader);
}
add a comment |
add "throws IOException" to your method like this:
public static void main(String args) throws IOException{
FileReader reader=new FileReader("db.properties");
Properties p=new Properties();
p.load(reader);
}
add a comment |
add "throws IOException" to your method like this:
public static void main(String args) throws IOException{
FileReader reader=new FileReader("db.properties");
Properties p=new Properties();
p.load(reader);
}
add "throws IOException" to your method like this:
public static void main(String args) throws IOException{
FileReader reader=new FileReader("db.properties");
Properties p=new Properties();
p.load(reader);
}
answered Feb 7 '18 at 15:24
harun ugurharun ugur
39846
39846
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2305966%2fwhy-do-i-get-the-unhandled-exception-type-ioexception%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown