Java 7,try 和 catch 问题? Ubuntu Eclipse IDE

标签 java ubuntu

您好,我在使用下面的代码时遇到一些问题。 try 和 catch block 在我的 IDE 中的 try 行处有一个红色 X,告诉我遇到了“(”,但预期是“{”。我是否遗漏了某些内容?我的问题可能是什么?

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.StandardSocketOptions;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
/** * * @author Apress */

public class FutureForm 
{
public static void main(String[] args) 
{
    final int DEFAULT_PORT = 5555;
    final String IP = "127.0.0.1";
    // v-This parenthesis should be a bracket it says??
    try( AsynchronousServerSocketChannel asynchronousServerSocketChannel = AsynchronousServerSocketChannel.open()) 
    {
        if (asynchronousServerSocketChannel.isOpen()) 
        {
            asynchronousServerSocketChannel.bind(new InetSocketAddress(IP, DEFAULT_PORT));
            System.out.println("Waiting for connections ...");
            while (true) 
            {
                Future<AsynchronousSocketChannel> asynchronousSocketChannelFuture = asynchronousServerSocketChannel.accept();
                try (AsynchronousSocketChannel asynchronousSocketChannel = asynchronousSocketChannelFuture.get()) 
                {
                    System.out.println("Incoming connection from: " + asynchronousSocketChannel.getRemoteAddress());
                    final ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
                    while (asynchronousSocketChannel.read(buffer).get() != -1) 
                    {
                        buffer.flip();
                        asynchronousSocketChannel.write(buffer).get();
                        if (buffer.hasRemaining()) 
                            buffer.compact();
                        else 
                            buffer.clear();
                    }
                    System.out.println(asynchronousSocketChannel.getRemoteAddress() + " was successfully served!");
                } 
                catch (IOException || InterruptedException || ExecutionException ex) 
                {
                    System.err.println(ex);
                }
            }
        } 
        else 
        {
            System.out.println("The asynchronous server-socket channel cannot be opened!");
        }
    } 
    catch (IOException ex) 
    {
        System.err.println(ex);
    }
}
}

最佳答案

不确定这是否是警告的原因,但您的 multicatch 中存在语法错误:您只需使用单个管道。

catch (IOException | InterruptedException | ExecutionException ex)

关于Java 7,try 和 catch 问题? Ubuntu Eclipse IDE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10818372/

相关文章:

java - 将一个类中的 String 构造函数转换为另一个类中的按钮

java - junit:使用 forkMode ="once"时如何避免误报?

java - DBUnit 和 Oracle JDBC (ClassCastException)

python - Ubuntu 一个文件夹同步过滤器

c++ - 打开CV : CUDA context initialization for different methods

即使在设置了 include_path 之后,PHP 也包含并且不需要在 ubuntu 中工作

java - 转换为 java 无符号类型

java - 示例 Google 日历 API - 纯 Java

c++ - 段错误(核心转储)-Xubuntu

linux - scp 传输后禁止访问网站