[RESOLVIDO]Uso de Callable<Throwable>

3 respostas
wilsontads

Eu estou tentando usar o Callable pra retornar as excessões pra minha classe principal, vi muitos exemplos utilizando String,Long,etc…
mais não encontrei nada parecido com tratamento de excessões na rede.
Alguém aqui no fórum tem algum exemplo de como utilizar o Callable<Throwable>?

3 Respostas

wilsontads

Eu implementei algo, mais como é o primeiro, gostaria que vocês dessem uma olhada p/ ver se estou fazendo corretamente, o que posso melhorar..

public class Teste {

    public static class WordLengthCallable implements Callable&lt;Throwable&gt; {

        private Throwable thr;

        public WordLengthCallable() {
try {
            for(int i=0; i&lt;=10;i++){
            System.out.print(&quot;|&quot;);
            }
   } catch (Exception ex) {
                thr = ex;
            }
        }

        @Override
        public Throwable call() {
            return thr;
        }
    }

    public static void main(String args[]) throws Exception{
        Future&lt;Throwable&gt; futurethr=null;
        ExecutorService pool=null;
        try {
            pool = Executors.newCachedThreadPool();

            Callable&lt;Throwable&gt; callable = new WordLengthCallable();
            futurethr = pool.submit(callable);



        } catch (Exception ex) {
            System.out.println("Erro l: "+ex.getMessage()+"\nErro e: "+futurethr.get().getMessage());
            
        } finally {
            
            pool.shutdown();

        }
    }}
wilsontads

Fiz da seguinte forma.. só que quando entra no try do meu método main, o método get() está nulo..

public static class WordLengthCallable implements Callable&lt;Throwable&gt; {

        private Throwable thr;

        public WordLengthCallable() throws Exception{
            try {
                String vlr = null;
                
                for (int b = 0; b &lt;= 6; b++) {
                    if(vlr.equalsIgnoreCase(&quot;&quot;)){}
                }
            } catch (Exception ex) {
                thr = ex;
                throw new Exception();
              
                
            }
        }

        @Override
        public Throwable call() {
            return thr;
        }
    }

    public static void main(String args[]) throws Exception {
        Future&lt;Throwable&gt; futurethr = null;
        ExecutorService pool = null;

        try {
            pool = Executors.newCachedThreadPool();

            Callable&lt;Throwable&gt; callable = new WordLengthCallable();
            futurethr = pool.submit(callable);



        } catch (Exception ex) {
            System.out.println("Erro l: " + ex.getMessage() + "\nErro e: " + futurethr.get().getMessage());

        } finally {

            pool.shutdown();

        }
    }
}
wilsontads
public class Teste {

    public static class methodTest implements Callable&lt;Throwable&gt; {

        private Throwable thr = null;
        private List&lt;String&gt; list = new LinkedList&lt;String&gt;();

        public methodTest() throws Exception {
            try {
                list.add("wilson");
                list.add("moraes");
                //Aqui erro  Index: 2, Size: 2
                list.get(2);
                

            } catch (Exception ex) {
                thr = ex;
            }
        }

        @Override
        public Throwable call() {
            return thr;
        }
    }

    public static void main(String args[]) throws Exception {
        List&lt;String&gt; list = new LinkedList&lt;String&gt;();
        ExecutorService pool = null;

        try {
            pool = Executors.newCachedThreadPool();

            Callable&lt;Throwable&gt; callable = new methodTest();
            Future&lt;Throwable&gt; futurethr = pool.submit(callable);

            list.add("wilson");
            list.add("moraes");
           // list.get(2);
            



            if (futurethr.get() != null) {
                System.out.println("\nErro Externo: " + futurethr.get().getMessage());
            }
        } catch (Exception ex) {
            System.out.println("\nErro local: " + ex.getMessage());

        } finally {

            pool.shutdown();

        }
    }
    //RESULTADO: Erro Externo: Index: 2, Size: 2
}
Criado 17 de novembro de 2011
Ultima resposta 17 de nov. de 2011
Respostas 3
Participantes 1