Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch exception thrown by another thread in Java? [duplicate]

I'm using a library that creates its own thread and it throws an exception. How can I catch that exception? The exception is thrown on the line marked below:

ResourceDescriptor rd = new ResourceDescriptor();
        rd.setWsType(ResourceDescriptor.TYPE_FOLDER);
        fullUri += "/" + token;
        System.out.println(fullUri);
        // >>> EXCEPTION THROWN ON THE FOLLOWING LINE <<<
        rd.setUriString(fullUri.replaceAll("_", ""));
        try{
            rd = server.getWSClient().get(rd, null);
        }catch(Exception e){
            if(e.getMessage().contains("resource was not found")){
                this.addFolder(fullUri, label, false);
                System.out.println("Folder does not exist, will be added now.");
            }else{
                System.out.println("Error Messages: " + e.getMessage());
            }
        }
like image 968
yuejdesigner85 Avatar asked Apr 27 '12 13:04

yuejdesigner85


1 Answers

If you can not catch it maybe that helps you:

If you have the Thread object you can try to set a UncaughtExceptionHandler. Take a look at Thread.setUncaughtExceptionHandler(...).

Give us some more detail about the library you use and how you use it.

like image 86
Ortwin Angermeier Avatar answered Oct 01 '22 08:10

Ortwin Angermeier