简体   繁体   English

未报告的异常java.io.IOException

[英]unreported exception java.io.IOException

What's wrong with this code 这段代码有什么问题

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 *
 * @author Master
 */
public class Server {
    try
    {
    ServerSocket S = new ServerSocket(3333);
    Socket So = S.accept();
    }
    catch(IOException e)
    {
        System.out.println("IOError");
    }

}

Firstly I wrote the code without try catch and I got an unreported exception java.io.IOException; must be caught or declared to be thrown 首先,我没有尝试捕获就编写了代码,但遇到了unreported exception java.io.IOException; must be caught or declared to be thrown unreported exception java.io.IOException; must be caught or declared to be thrown Error but Netbeans didn't suggest that I add a try-catch block . unreported exception java.io.IOException; must be caught or declared to be thrown错误,但是Netbeans不建议我添加try-catch块。 Now I added the try-catch block manually but It still shows an error and suggests that I must add another try-catch block ! 现在,我手动添加了try-catch块,但是它仍然显示错误,建议我必须添加另一个try-catch块!

替代文字

You're trying to add a try block at the top level of the class - you can't do that. 您试图在类的顶层添加一个try块-您不能这样做。 Try blocks have to be in methods or initializer blocks. try块必须位于方法或初始化程序块中。

If you really want to create the server socket on construction, put the code in a constructor: 如果您真的想在构造时创建服务器套接字,请将代码放入构造函数中:

public class Server {

    private ServerSocket serverSocket;
    private Socket firstConnection;

    public Server {
        try
        {
            serverSocket = new ServerSocket(3333);
            firstConnection = serverSocket.accept();
        }
        catch(IOException e)
        {
            System.out.println("IOError");
        }
    }
}

I assume you'll have real exception handling (or rethrowing) rather than just catching the IOException and continuing, of course? 我假设您将拥有真正的异常处理(或重新抛出),而不是仅仅捕获IOException并继续执行,当然吗?

This is not valid Java. 这不是有效的Java。 This code needs to be in a block, method, or constructor. 此代码必须位于块,方法或构造函数中。

From the screenshot of your IDE, the error "must be caught or declared to be thrown" is not the only error you have. 在您的IDE的屏幕截图中,错误“必须被捕获或声明为引发”不是唯一的错误。

When you have syntax that is this far off, the compiler will likely report several errors, some of which are side-effects of other errors - like not enclosing this code in a method (I'm sure the red X next to the catch block is reporting a similar error). 当您的语法相差太远时,编译器可能会报告一些错误,其中一些是其他错误的副作用-例如未将此代码包含在方法中(我敢肯定catch块旁边的红色X正在报告类似的错误)。

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

相关问题 actionlistener上未报告的异常java.io.IOException - unreported exception java.io.IOException at actionlistener 编译Java代码时出现未报告的异常java.io.IOException - Unreported exception java.io.IOException when compiling Java code 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 未处理的异常java.io.iOException - Unhandled Exception java.io.iOException java.io.IOException java - java.io.IOException java 无法解决异常java.io.IOException:URL的403 - unable to resolve the exception java.io.IOException: 403 for URL java.io.IOException:使用 SpringBoot 写入 Multipart 时出现异常 - java.io.IOException: Exception writing Multipart with SpringBoot 线程“main”中的异常 java.io.IOException:作业失败! 在 mapreduce 中 - Exception in thread "main" java.io.IOException: Job failed! in mapreduce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM