Results 1 to 3 of 3
  1. #1
    Josep_16's Avatar
    Josep_16 is offline Member
    Join Date
    Mar 2011
    Posts
    40
    Rep Power
    0

    Default What's the point of try {} ?

    Hello. I was looking at a tutorial talking about multi-threads, and they used this example to explain the subject:

    Java Code:
    /**
     * Applet MostrarCadena
     *
     * <APPLET CODE="MostrarCadena.class" WIDTH="200" HEIGHT="70">
     *   <PARAM NAME="Cadena" VALUE="Esto sí que es chulo">
     * </APPLET>
     */
    
    import java.applet.Applet;
    import java.awt.*;
    
    public class MostrarCadena extends Applet implements Runnable {
      String mensaje;
      int lugar;
      Thread hilo = null;
      public void init() {
        mensaje = getParameter("cadena");
        if (mensaje==null)
          mensaje = "Mensaje por defecto";
        lugar = getBounds().width;
      }
      public void start() {
        if (hilo==null) {
          hilo = new Thread(this);
          hilo.start();
        }
      }
      public void stop() {
        hilo = null;
      }
      public void paint(Graphics g) {
        g.drawString(mensaje,lugar,20);
      }
      public void run() {
        while (hilo!=null && hilo.isAlive()) {
          lugar -= 1;
          repaint();
          catch (InterruptedException e) {} // No hacemos nada
        }
      }
    }
    Looking at the run() method, I didn't understand why is try {} needed (
    Java Code:
    try {
            hilo.sleep(10);
          }
    )

    In fact, I think that I don't even know what's try{} for. I'd like someone to explain me what's its function.

    THANKS.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    comment out the try{}, leave the sleep() and compile your program. The compiler will give an error message explaining why it wants the try{}

Similar Threads

  1. What is the point of Interfaces?
    By minime12358 in forum New To Java
    Replies: 9
    Last Post: 09-02-2010, 02:24 PM
  2. looking for Point interpolater
    By gib65 in forum AWT / Swing
    Replies: 5
    Last Post: 08-20-2010, 04:22 PM
  3. Access Point
    By ja107 in forum Networking
    Replies: 0
    Last Post: 03-05-2010, 05:59 PM
  4. Can someone point me to an example?
    By smhjr in forum New To Java
    Replies: 2
    Last Post: 08-27-2009, 11:38 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •