Better exception handling.
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Sat, 10 Mar 2018 12:41:46 +0000 (13:41 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Sat, 10 Mar 2018 12:41:46 +0000 (13:41 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
TD1/exo2/Main.java
TD1/exo2/ThreadJob.java
TD1/exo3/BufferCirc.java
TD1/exo3/Consommateur.java
TD1/exo3/Producteur.java
TD2/ClientSimplifie.java

index f44796019d37fa664cd3e2ff426b094b85a84665..e97322de725a477bc812a1caf8f702102c81bb42 100644 (file)
@@ -16,6 +16,7 @@ class Main {
             }
             catch (InterruptedException e) {
                 // this part is executed when an exception (in this example InterruptedException) occurs
+                System.out.println("InterruptedException: " + e);
             }
         }
         System.out.println("Threads execution finished");
index 32a7365f2a66de51747fba74a6eb18f282641a65..9b0f99eeba67d1954373e44eec5297c1525d8c0d 100644 (file)
@@ -18,6 +18,7 @@ public class ThreadJob implements Runnable {
             }
             catch(InterruptedException e) {
                 // this part is executed when an exception (in this example InterruptedException) occurs
+                System.out.println("InterruptedException: " + e);
             }
             String threadName = Thread.currentThread().getName();
             System.out.println(threadName + " has slept for " + sleep_time + " ms for the " + (j + 1) + " times");
index e03e8ea950e2cdd3ab244bde76454722f2abbd90..486951502caffd1d4925291dfa37eb1d74372f74 100644 (file)
@@ -36,7 +36,9 @@ public class BufferCirc {
                                                                   + " is waiting, size: " + nbObj);
                                wait();
                        }
-                       catch (InterruptedException e) {}
+                       catch (InterruptedException e) {
+                               System.out.println("InterruptedException: " + e);
+                       }
                }
                nbObj++;
                tampon[prem] = obj;
@@ -53,7 +55,9 @@ public class BufferCirc {
                                                                   + " is waiting, size: " + nbObj);
                                wait();
                        }
-                       catch (InterruptedException e) {}
+                       catch (InterruptedException e) {
+                               System.out.println("InterruptedException: " + e);
+                       }
                }
                Object outObj = null;
                nbObj--;
index 480134976bcecd810a20ae7db3fdfbdfc4fb0eb4..43c811ab6895e80e7f1cfac5dc092aeadadc04c3 100644 (file)
@@ -26,7 +26,9 @@ public class Consommateur implements Runnable {
                        try {
                                Thread.sleep(ThreadLocalRandom.current().nextInt(1001));
                        }
-                       catch (InterruptedException e) {}
+                       catch (InterruptedException e) {
+                               System.out.println("InterruptedException: " + e);
+                       }
                }
        }
 
index 47a402180d5f8c9a6fa4854bc4b3b03f76c97858..f3cc123ac18ba582eba727a017023a0e29b176ee 100644 (file)
@@ -28,7 +28,9 @@ public class Producteur implements Runnable {
                        try {
                                Thread.sleep(ThreadLocalRandom.current().nextInt(101));
                        }
-                       catch (InterruptedException e) {}
+                       catch (InterruptedException e) {
+                               System.out.println("InterruptedException: " + e);
+                       }
                }
        }
 
index f5ccb40933f3f1f315b6de53588d228971193701..fd3cd7c9162dc3937d1a397b1c1cf0489ffe819e 100644 (file)
@@ -26,12 +26,16 @@ public class ClientSimplifie {
         try {
             sock = new Socket(adresseIPServeur, portServeur);
         }
-        catch (IOException e) {}
+        catch (IOException e) {
+            System.out.println("IOException: " + e);
+        }
         InputStream IStream = null;
         try {
             IStream = sock.getInputStream();
         }
-        catch (IOException e) {}
+        catch (IOException e) {
+            System.out.println("IOException: " + e);
+        }
         InputStreamReader IMesg = new InputStreamReader(IStream);
         lecture = new BufferedReader(IMesg);
 
@@ -39,31 +43,48 @@ public class ClientSimplifie {
         try {
             OStream = sock.getOutputStream();
         }
-        catch (IOException e) {}
+        catch (IOException e) {
+            System.out.println("IOException: " + e);
+        }
         ecriture = new PrintWriter(OStream);
     }
 
+    /**
+     * Send a message on the opened client socket
+     * @param msg a string containing the message to send
+     */
     public void sendMsg(String msg) {
         ecriture.println(msg);
         ecriture.flush();
     }
 
+    /**
+     * Receive a message sent on the opened client socket
+     * @return a string containing the received message
+     */
     public String receiveMsg() {
         String line = new String();
         try {
             //FIXME: read only the line before the ending newline
             line = lecture.readLine();
         }
-        catch (IOException e) {}
+        catch (IOException e) {
+            System.out.println("IOException: " + e);
+        }
         return line;
     }
 
+    /**
+     * Close all opened I/O streams attached to this object instance
+     */
     public void closeRWIO() {
         ecriture.close();
         try {
             lecture.close();
         }
-        catch (IOException e) {}
+        catch (IOException e) {
+            System.out.println("IOException: " + e);
+        }
     }
 
 } // fin classe ClientSimplifie