Foros del Web » Programación para mayores de 30 ;) » Java »

Ayuda con una Exception!

Estas en el tema de Ayuda con una Exception! en el foro de Java en Foros del Web. Hola, no se por que razón me marca la siguiente exception existe.java:14: unreported exception java.io.IOException; must be caught or declared to be thrown busca(); ^ ...
  #1 (permalink)  
Antiguo 11/03/2009, 13:07
 
Fecha de Ingreso: marzo-2008
Mensajes: 63
Antigüedad: 16 años, 1 mes
Puntos: 0
Pregunta Ayuda con una Exception!

Hola, no se por que razón me marca la siguiente exception

existe.java:14: unreported exception java.io.IOException; must be caught or declared to be thrown
busca();
^

ya intente varias cosas pero no he logrado que funcione, por favor ayuda.

Código:
import java.io.*;
import java.util.*;

class existe extends Thread
{
   private boolean running = true;
   
   public existe() {}
   
   public void run()
   {
      while (running)
      {
         busca();
     
         try
         {
            System.out.println("");
            System.out.println("Realizando busqueda de actualización...");
            Thread.sleep(10000);
         }
         catch (InterruptedException e)
         {
            running = false;
         }
       }
   }
   
   public static void main(String[] args) throws IOException
   {
      new existe().start(); 
   }
   
   public static void busca() throws IOException
   {
      try
      {
         String sFichero = "Eambascu.tst";
         File fichero = new File(sFichero);
          
         String cad1 = "flag.txt";
         FileInputStream tream = new FileInputStream(cad1);
         BufferedReader in = new BufferedReader(new InputStreamReader(tream));
         String line = in.readLine();
         String line2 = "0";
         
         if (fichero.exists())
         {
            System.out.println("El archivo " + sFichero + " existe");
            
            if (line.equals(line2))
            {   
               String cad = "flag.txt";
               File TextFile = new File(cad);
               FileWriter TextOut;
               try
               {
                  TextOut = new FileWriter(TextFile);
                  TextOut.write("1");
                  System.out.println("Se esta transmitiendo el archivo...");
                  TextOut.close();
               }
               catch(IOException e)
               {
                  e.printStackTrace();
               }
                   
               System.out.println("Trabajando con la bandera");
               
               String cad2 = "flag.txt";
               File TextFile1 = new File(cad2);
               FileWriter TextOut1;
               try
               {
                  TextOut1 = new FileWriter(TextFile1);
                  TextOut1.write("0");
                  System.out.println("Y se puede usar");
                  TextOut1.close();
               }
               catch(IOException e)
               {
                  e.printStackTrace();
               }
            } 
            else
            {
               System.out.println("El archivo esta siendo usado");
            }   
         }  
         else
         {
            System.out.println("El archivo no se encuentra");
         }
         limpiar(50-2);
      }
      catch (IOException e) 
      {
         throw new IOException(e.getMessage());
      }
   }
      
   public static void limpiar(int lineas)
   {
      for (int i=0; i < lineas; i++)
      {
         System.out.println();
      }
   }
}
Saludos.
  #2 (permalink)  
Antiguo 11/03/2009, 13:43
Avatar de drac94  
Fecha de Ingreso: mayo-2008
Ubicación: México
Mensajes: 383
Antigüedad: 15 años, 11 meses
Puntos: 5
Respuesta: Ayuda con una Exception!

pon la funcion adentro del try, por que tu funcion lanza una exepcion, pero del otro lado no sabe como manejarla
  #3 (permalink)  
Antiguo 11/03/2009, 13:53
 
Fecha de Ingreso: octubre-2005
Ubicación: Córdoba, Veracruz
Mensajes: 42
Antigüedad: 18 años, 6 meses
Puntos: 0
Respuesta: Ayuda con una Exception!

Lo que sucede es que, estás diciendo que tu método busca() lanza la excepción IOException, así que deberías cacharla en algún lugar.
Modifiqué un poco el código que pegaste, a ver si así te funciona bien (las líneas que agregué/modifiqué tienen comentario a la derecha):

Código:
import java.io.*;
import java.util.*;

class existe extends Thread
{
   private boolean running = true;
   
   public existe() {}
   
   public void run()
   {
      while (running)
      {
         try{                                    
          busca();                      //mueves el busca() dentro del try
          System.out.println("");
          System.out.println("Realizando busqueda de actualización...");
          Thread.sleep(10000);
         }
         catch (InterruptedException e)
         {
            running = false;
         }
         catch (IOException e){           //aquí estás cachando la excepción de busca()
                                          //aquí deberías hacer algo con esa excepción (¿mostrar un mensaje, quizá?
         }
       }
   }
   
   public static void main(String[] args) throws IOException
   {
      new existe().start(); 
   }
   
   public static void busca() throws IOException
   {
      try
      {
         String sFichero = "Eambascu.tst";
         File fichero = new File(sFichero);
          
         String cad1 = "flag.txt";
         FileInputStream tream = new FileInputStream(cad1);
         BufferedReader in = new BufferedReader(new InputStreamReader(tream));
         String line = in.readLine();
         String line2 = "0";
         
         if (fichero.exists())
         {
            System.out.println("El archivo " + sFichero + " existe");
            
            if (line.equals(line2))
            {   
               String cad = "flag.txt";
               File TextFile = new File(cad);
               FileWriter TextOut;
               try
               {
                  TextOut = new FileWriter(TextFile);
                  TextOut.write("1");
                  System.out.println("Se esta transmitiendo el archivo...");
                  TextOut.close();
               }
               catch(IOException e)
               {
                  e.printStackTrace();
               }
                   
               System.out.println("Trabajando con la bandera");
               
               String cad2 = "flag.txt";
               File TextFile1 = new File(cad2);
               FileWriter TextOut1;
               try
               {
                  TextOut1 = new FileWriter(TextFile1);
                  TextOut1.write("0");
                  System.out.println("Y se puede usar");
                  TextOut1.close();
               }
               catch(IOException e)
               {
                  e.printStackTrace();
               }
            } 
            else
            {
               System.out.println("El archivo esta siendo usado");
            }   
         }  
         else
         {
            System.out.println("El archivo no se encuentra");
         }
         limpiar(50-2);
      }
      catch (IOException e) 
      {
         throw new IOException(e.getMessage());
      }
   }
      
   public static void limpiar(int lineas)
   {
      for (int i=0; i < lineas; i++)
      {
         System.out.println();
      }
   }
}
Si sigue sin funcionar escribe de nuevo, a ver si puedo ayudarte.
¡Saludos!
  #4 (permalink)  
Antiguo 11/03/2009, 14:04
 
Fecha de Ingreso: marzo-2008
Mensajes: 63
Antigüedad: 16 años, 1 mes
Puntos: 0
Respuesta: Ayuda con una Exception!

Muchas gracias, si funcionó.
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 19:38.