Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9000465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:18:52+00:00 2026-06-16T00:18:52+00:00

I’ve the next code, based on an example to use Java UDPSocket() (not built-in

  • 0

I’ve the next code, based on an example to use Java UDPSocket() (not built-in a class):

package BomberButti;
import java.io.*; 
import java.net.*; 

/**
 * BomberServer
 * Hier wordt de Server opgezet waarop Clients kunnen connecteren om vervolgens tegen elkaar te spelen
 * @author Kaj
 */
public class BomberServer {

    public BomberServer() {
        waitForPlayers();
    }

    public static void waitForPlayers() throws Exception {
        try {
            DatagramSocket serverSocket = new DatagramSocket(9876); //Socket openen

            byte[] receiveData = new byte[1024]; //Ontvangen gegevens
            byte[] sendData  = new byte[1024];  //Te verzenden gegevens

            while(true) {
                receiveData = new byte[1024]; 

                //Ontvangen data (als die er is) ophalen
                DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); 

                System.out.println ("Waiting for datagram packet");

                serverSocket.receive(receivePacket); 

                String sentence = new String(receivePacket.getData()); 

                InetAddress IPAddress = receivePacket.getAddress(); 

                int port = receivePacket.getPort(); 

                System.out.println ("From: " + IPAddress + ":" + port);
                System.out.println ("Message: " + sentence);

                String capitalizedSentence = sentence.toUpperCase(); 

                sendData = capitalizedSentence.getBytes(); 
                sendData = new String("Request accepted").getBytes();
                DatagramPacket sendPacket = 
                   new DatagramPacket(sendData, sendData.length, IPAddress, 
                                     port); 

                serverSocket.send(sendPacket); 

              } 
        }
        catch(SocketException ex) {
            System.out.println("UDP Port 9876 is occupied.");
            System.exit(1);
        }
    }
    public static void main(String args[])  {
        BomberServer server = new BomberServer();
    }
}

When trying to run this code I get the error ‘unreported exception Exception; must be caught er declared to be thrown’.

I understand that this has to do with the ‘throws Exception’ I placed behind the declaration of my waitForPlayers() method. But when I remove this throws Exception part, I get an error at the line: serverSocket.receive(receivePacket); ‘unreporter exception IOException; must be declared to be thrown’ so I guess none of the both ways I tried is the right way.

So how do I have to do it that it’s correct?

Sincerely,

Luxo

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:18:53+00:00Added an answer on June 16, 2026 at 12:18 am

    The code inside waitForPlayers() can throw an IOException, so the method should declare that it throws an IOException (and not an Exception, because Exception is too vague):

    public static void waitForPlayers() throws IOException {
    

    Since this method can throw an IOException, and you call this method from the constructor without catching IOException, the constructor must also declare that it throws IOException:

    public BomberServer() throws IOException {
    

    And of course, the method which calls this constructor will also have to either catch IOException, or declare that it throws IOException, etc. etc. until you get to a point where you can handle this IOException, and thus catch it to handle it.

    Read the tutorial about exceptions.

    Important note: catching an exception, printing its stack trace, and continuing as if nothing happened is generally not how an exception should be handled.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I am trying to understand how to use SyndicationItem to display feed which is
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.