简体   繁体   English

test1.java:10: 错误:未报告的异常 IOException; 必须被捕获或声明被抛出 char ch = (char)br.read(); ^

[英]test1.java:10: error: unreported exception IOException; must be caught or declared to be thrown char ch = (char)br.read(); ^

Any help will be appreciated任何帮助将不胜感激

My code:我的代码:

   import java.io.*;
   import java.util.Scanner;

class Accept {类接受{

public static void main(String args[])
{ 
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Enter: ");
    char ch = br.read();
    System.out.print("Entered is: " + ch);
}

} }

Just like the error says, you have to either catch or throw the IOException thrown by mentioned method.就像错误所说的那样,您必须捕获或抛出上述方法抛出的IOException

You can throw it by:您可以通过以下方式抛出它:

public static void main(String args[]) throws IOException
{ 
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Enter: ");
    char ch = (char) br.read();
    System.out.print("Entered is: " + ch);
}

Java tutorials is a good place to start learning about Java Exceptions. Java 教程是开始学习 Java 异常的好地方。

you can also try this:你也可以试试这个:

  public static void main(String[] args) {
      try {
          BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
          System.out.print("Enter: ");
          char ch = (char) br.read();
          System.out.print("Entered is: " + ch);
      }catch(IOException ex) {
        Logger.getLogger(State.class.getName()).log(Level.SEVERE, null, ex);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java错误:必须捕获未声明的异常ioexception或将其声明为引发 - Java error: unreported exception ioexception must be caught or declared to be thrown Java错误未报告的异常IOException; 必须被抓住或宣布被抛出 - Java Error unreported exception IOException; must be caught or declared to be thrown 未报告的异常IOException,必须捕获或声明为抛出 - Unreported exception IOException, must be caught or declared to be thrown 错误:未报告的异常IOException; 必须被捕获或声明被抛出流() - error: unreported exception IOException; must be caught or declared to be thrown in stream() Java类中的错误“必须捕获或声明抛出未报告的异常java.io.ioexception” - Error “unreported exception java.io.ioexception must be caught or declared to be thrown” in Java class 错误消息“未报告的异常 java.io.IOException;必须被捕获或声明为抛出” - Error message "unreported exception java.io.IOException; must be caught or declared to be thrown" 错误:未报告的异常java.io.IOException; 必须被抓住或宣布被抛出 - ERROR: unreported exception java.io.IOException; must be caught or declared to be thrown 未报告的异常 ParseException; 必须被捕获或声明被抛出 -- JAVA 错误 - unreported exception ParseException; must be caught or declared to be thrown -- JAVA Error 未报告的例外例外; 必须被抓住或宣布被抛出 - unreported exception Exception; must be caught or declared to be thrown Java:未报告的异常Exception; 必须被抓住或宣布被扔掉 - Java: Unreported exception Exception; must be caught or declared to be thrown
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM