Handling Exceptions in Networking Example
This program demonstrates handling exceptions in networking.
Problem Statement
Write a Java program that handles IOException during socket connection.
Source Code
| 1 | import java.net.*; |
| 2 | import java.io.*; |
| 3 | public class NetworkingExceptionExample { |
| 4 | public static void main(String[] args) { |
| 5 | System.out.println("Eduinq Networking Exception Example"); |
| 6 | try(Socket s=new Socket("localhost",9999)) { |
| 7 | System.out.println("Eduinq Connected"); |
| 8 | } catch(IOException e) { |
| 9 | System.out.println("Eduinq Exception: Network Error - "+e.getMessage()); |
| 10 | } |
| 11 | } |
| 12 | } |
Program Output
Eduinq Networking Exception Example Eduinq Exception: Network Error - Connection refused
Explanation
Networking operations may fail due to unavailable servers or ports. - IOException ensures errors are caught and reported. - Provides meaningful feedback for debugging. - In enterprise systems, networking exception handling ensures reliable communication. Conclusion: Shows handling exceptions in networking.