From 7293fe6dc670d5164859159aab339fa3a4750773 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 10 Mar 2018 13:41:46 +0100 Subject: [PATCH] Better exception handling. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- TD1/exo2/Main.java | 1 + TD1/exo2/ThreadJob.java | 1 + TD1/exo3/BufferCirc.java | 8 ++++++-- TD1/exo3/Consommateur.java | 4 +++- TD1/exo3/Producteur.java | 4 +++- TD2/ClientSimplifie.java | 31 ++++++++++++++++++++++++++----- 6 files changed, 40 insertions(+), 9 deletions(-) diff --git a/TD1/exo2/Main.java b/TD1/exo2/Main.java index f447960..e97322d 100644 --- a/TD1/exo2/Main.java +++ b/TD1/exo2/Main.java @@ -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"); diff --git a/TD1/exo2/ThreadJob.java b/TD1/exo2/ThreadJob.java index 32a7365..9b0f99e 100644 --- a/TD1/exo2/ThreadJob.java +++ b/TD1/exo2/ThreadJob.java @@ -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"); diff --git a/TD1/exo3/BufferCirc.java b/TD1/exo3/BufferCirc.java index e03e8ea..4869515 100644 --- a/TD1/exo3/BufferCirc.java +++ b/TD1/exo3/BufferCirc.java @@ -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--; diff --git a/TD1/exo3/Consommateur.java b/TD1/exo3/Consommateur.java index 4801349..43c811a 100644 --- a/TD1/exo3/Consommateur.java +++ b/TD1/exo3/Consommateur.java @@ -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); + } } } diff --git a/TD1/exo3/Producteur.java b/TD1/exo3/Producteur.java index 47a4021..f3cc123 100644 --- a/TD1/exo3/Producteur.java +++ b/TD1/exo3/Producteur.java @@ -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); + } } } diff --git a/TD2/ClientSimplifie.java b/TD2/ClientSimplifie.java index f5ccb40..fd3cd7c 100644 --- a/TD2/ClientSimplifie.java +++ b/TD2/ClientSimplifie.java @@ -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 -- 2.34.1