Handling Exceptions in REST Client Example
This program demonstrates handling exceptions in REST clients.
Problem Statement
Write a Java program that handles exceptions during REST client calls.
Source Code
| 1 | import java.net.*; |
| 2 | import java.io.*; |
| 3 | public class RestClientExceptionExample { |
| 4 | public static void main(String[] args) { |
| 5 | System.out.println("Eduinq REST Client Exception Example"); |
| 6 | try { |
| 7 | URL url=new URL("http://localhost:9999/api"); |
| 8 | BufferedReader br=new BufferedReader(new InputStreamReader(url.openStream())); |
| 9 | System.out.println("Eduinq Response: "+br.readLine()); |
| 10 | } catch(IOException e) { |
| 11 | System.out.println("Eduinq Exception: REST Client Error - "+e.getMessage()); |
| 12 | } |
| 13 | } |
| 14 | } |
Program Output
Eduinq REST Client Exception Example Eduinq Exception: REST Client Error - Connection refused
Explanation
REST client calls may fail due to network or server issues. - IOException ensures errors are caught and reported. - Provides meaningful feedback for debugging. - In enterprise systems, REST client exception handling ensures reliable API communication. Conclusion: Shows handling exceptions in REST clients.