Example usage for java.lang Thread.UncaughtExceptionHandler Thread.UncaughtExceptionHandler

List of usage examples for java.lang Thread.UncaughtExceptionHandler Thread.UncaughtExceptionHandler

Introduction

In this page you can find the example usage for java.lang Thread.UncaughtExceptionHandler Thread.UncaughtExceptionHandler.

Prototype

Thread.UncaughtExceptionHandler

Source Link

Usage

From source file:com.apptentive.android.sdk.Apptentive.java

private synchronized static void asyncFetchConversationToken(final Context context) {
    Thread thread = new Thread() {
        @Override/*from   w  w w .j a va  2  s . c  o  m*/
        public void run() {
            fetchConversationToken(context);
        }
    };
    Thread.UncaughtExceptionHandler handler = new Thread.UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(Thread thread, Throwable throwable) {
            Log.w("Caught UncaughtException in thread \"%s\"", throwable, thread.getName());
            MetricModule.sendError(context.getApplicationContext(), throwable, null, null);
        }
    };
    thread.setUncaughtExceptionHandler(handler);
    thread.setName("Apptentive-FetchConversationToken");
    thread.start();
}

From source file:brainflow.app.toplevel.BrainFlow.java

private void initExceptionHandler() {
    LookAndFeelFactory.UIDefaultsCustomizer uiDefaultsCustomizer = new LookAndFeelFactory.UIDefaultsCustomizer() {
        public void customize(UIDefaults defaults) {
            ThemePainter painter = (ThemePainter) UIDefaultsLookup.get("Theme.painter");
            defaults.put("OptionPaneUI", "com.jidesoft.plaf.basic.BasicJideOptionPaneUI");

            defaults.put("OptionPane.showBanner", Boolean.TRUE); // show banner or not. default is true
            //defaults.put("OptionPane.bannerIcon", JideIconsFactory.getImageIcon(JideIconsFactory.JIDE50));
            defaults.put("OptionPane.bannerFontSize", 13);
            defaults.put("OptionPane.bannerFontStyle", Font.BOLD);
            defaults.put("OptionPane.bannerMaxCharsPerLine", 60);
            defaults.put("OptionPane.bannerForeground",
                    painter != null ? painter.getOptionPaneBannerForeground() : null); // you should adjust this if banner background is not the default gradient paint
            defaults.put("OptionPane.bannerBorder", null); // use default border

            // set both bannerBackgroundDk and // set both bannerBackgroundLt to null if you don't want gradient
            defaults.put("OptionPane.bannerBackgroundDk",
                    painter != null ? painter.getOptionPaneBannerDk() : null);
            defaults.put("OptionPane.bannerBackgroundLt",
                    painter != null ? painter.getOptionPaneBannerLt() : null);
            defaults.put("OptionPane.bannerBackgroundDirection", Boolean.TRUE); // default is true

            // optionally, you can set a Paint object for BannerPanel. If so, the three UIDefaults related to banner background above will be ignored.
            defaults.put("OptionPane.bannerBackgroundPaint", null);

            defaults.put("OptionPane.buttonAreaBorder", BorderFactory.createEmptyBorder(6, 6, 6, 6));
            defaults.put("OptionPane.buttonOrientation", SwingConstants.RIGHT);
        }//from   www  . ja  va  2s  .  com
    };
    uiDefaultsCustomizer.customize(UIManager.getDefaults());

    Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
        public void uncaughtException(Thread t, Throwable e) {
            e.printStackTrace();
            ExceptionDialog ed = new ExceptionDialog(e);
            JDialog dialog = ed.createDialog(brainFrame);
            dialog.setVisible(true);
        }
    });

}

From source file:com.apptentive.android.sdk.Apptentive.java

private static void asyncFetchAppConfiguration(final Context context) {
    Thread thread = new Thread() {
        public void run() {
            fetchAppConfiguration(context, GlobalInfo.isAppDebuggable);
        }/*from   w ww .j  av a 2  s  .co m*/
    };
    Thread.UncaughtExceptionHandler handler = new Thread.UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(Thread thread, Throwable throwable) {
            Log.e("Caught UncaughtException in thread \"%s\"", throwable, thread.getName());
            MetricModule.sendError(context.getApplicationContext(), throwable, null, null);
        }
    };
    thread.setUncaughtExceptionHandler(handler);
    thread.setName("Apptentive-FetchAppConfiguration");
    thread.start();
}

From source file:com.kdmanalytics.toif.assimilator.Assimilator.java

private void processKdmXmlFile(final List<File> kdmFiles)
        throws FileNotFoundException, IOException, RepositoryException, ToifException {
    if (debug) {/* ww w.j a  v a2 s. co m*/
        LOG.debug("processing kdm file...");
        //System.err.println("processing kdm file...");
    }

    PipedInputStream in = new PipedInputStream();
    final PipedOutputStream out = new PipedOutputStream(in);
    final ThreadStatus status = new ThreadStatus();

    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            KdmXmlHandler handler = null;
            try {
                if (kdmFiles.size() > 1) {
                    final String msg = "There should only be one .kdm file.";
                    LOG.error(msg);
                    throw new ToifException(msg);
                } else if (kdmFiles.size() == 1) {
                    File kdmFile = kdmFiles.get(0); // get the head of
                                                    // thelist.
                    handler = load(kdmFile, out);
                }
                out.flush();
                out.close();

                if (handler == null) {
                    return;
                }
                setNextId(handler.getNextId());
                setSmallestBigNumber(handler.getSmallestBigNumber());
                // increase
            } catch (IOException e) {
                final String msg = "IO exception whilst processing kdm file. "
                        + ". Possibly an existing kdm file is in your input path!";

                LOG.error(msg, e);
                status.exception = new ToifException(msg, e);
            } catch (RepositoryException e) {
                final String msg = "Repository Exception whilst processing kdm file. "
                        + ". Possibly an existing kdm file is in your input path!";

                LOG.error(msg, e);
                status.exception = new ToifException(msg, e);
            } catch (ToifException e) {
                // RJF final String msg =
                // "Processing Exception whilst processing kdm file. "
                // + ". Possibly that input file is invalid XML!";

                // LOG.error(msg, e);
                status.exception = e;
            } finally {
                if (out != null)
                    try {
                        out.close();
                    } catch (IOException e) {
                        // Just leave it alone
                        LOG.error("unable to close stream");
                    }
            }
        }
    });

    // ---------------------------------------------------------
    // Unable to change logic within the short time frame given so
    // adding a means to catch unknown exceptions in thread
    // ----------------------------------------------------------
    Thread.UncaughtExceptionHandler tueh = new Thread.UncaughtExceptionHandler() {

        public void uncaughtException(Thread th, Throwable ex) {
            LOG.error("Uncaught exception: " + ex);
            status.exception = (Exception) ex;
        }
    };

    t.setUncaughtExceptionHandler(tueh);
    t.start();

    streamStatementsToRepo(in);
    try {
        t.join();

        // Check if we enoutered exception during processing and
        // proxy throw if we have one
        if (status.exception != null) {
            // Leave alone if already a ToifException
            if (status.exception instanceof ToifException)
                throw (ToifException) status.exception;
            else
                throw new ToifException(status.exception);

        }
    } catch (InterruptedException e) {
        LOG.error("Interrupted");
        throw new ToifException("Interrupted");
    }
}

From source file:com.sentaroh.android.SMBSync2.SyncTaskUtility.java

@SuppressWarnings("unused")
private void testAuth(final NtlmPasswordAuthentication auth, final String host, String port,
        final NotifyEvent ntfy) {
    final UncaughtExceptionHandler defaultUEH = Thread.currentThread().getUncaughtExceptionHandler();
    Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
        @Override/*from   w ww  .j a va2s .c  om*/
        public void uncaughtException(Thread thread, Throwable ex) {
            Thread.currentThread().setUncaughtExceptionHandler(defaultUEH);
            ex.printStackTrace();
            StackTraceElement[] st = ex.getStackTrace();
            String st_msg = "";
            for (int i = 0; i < st.length; i++) {
                st_msg += "\n at " + st[i].getClassName() + "." + st[i].getMethodName() + "("
                        + st[i].getFileName() + ":" + st[i].getLineNumber() + ")";
            }
            String end_msg = ex.toString() + st_msg;
            ntfy.notifyToListener(true, new Object[] { end_msg });
            // re-throw critical exception further to the os (important)
            //                defaultUEH.uncaughtException(thread, ex);
        }
    });

    String err_msg = null;
    SmbFile sf = null;
    SmbFile[] lf = null;
    String url = "";
    if (port.equals("")) {
        url = "smb://" + host + "/IPC$/";
    } else {
        url = "smb://" + host + ":" + port + "/IPC$/";
    }
    //      Log.v("","url="+url);
    try {
        sf = new SmbFile(url, auth);
        sf.connect();
        util.addDebugMsg(1, "I",
                "Test logon completed, host=" + host + ", port=" + port + ", user=" + auth.getUsername());
    } catch (SmbException e) {
        String[] e_msg = NetworkUtil.analyzeNtStatusCode(e, mContext, url, auth.getUsername());
        err_msg = e_msg[0];
        util.addDebugMsg(1, "I", "Test logon failed." + "\n" + err_msg);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Thread.currentThread().setUncaughtExceptionHandler(defaultUEH);
    ntfy.notifyToListener(true, new Object[] { err_msg });
}

From source file:io.openvidu.test.e2e.OpenViduTestAppE2eTest.java

@Test
@DisplayName("Remote record cross-browser audio-only and video-only")
void remoteRecordAudioOnlyVideoOnlyTest() throws Exception {
    isRecordingTest = true;//from www .j ava2  s . c om

    setupBrowser("chromeAlternateScreenShare");

    log.info("Remote record cross-browser audio-only and video-only");

    final String SESSION_NAME = "TestSession";
    final String RECORDING_COMPOSED_VIDEO = "COMPOSED_VIDEO_ONLY";
    final String RECORDING_COMPOSED_AUDIO = "COMPOSED_AUDIO_ONLY";
    final String RECORDING_INDIVIDUAL_VIDEO = "INDIVIDUAL_VIDEO_ONLY";
    final String RECORDING_INDIVIDUAL_AUDIO = "INDIVIDUAL_AUDIO_ONLY";
    final int RECORDING_DURATION = 5000;

    Thread.UncaughtExceptionHandler h = new Thread.UncaughtExceptionHandler() {
        public void uncaughtException(Thread th, Throwable ex) {
            System.out.println("Uncaught exception: " + ex);
            synchronized (lock) {
                OpenViduTestAppE2eTest.ex = new Exception(ex);
            }
        }
    };

    Thread t = new Thread(() -> {
        MyUser user2 = new MyUser(new FirefoxUser("FirefoxUser", 30));
        otherUsers.add(user2);
        user2.getDriver().get(APP_URL);
        WebElement urlInput = user2.getDriver().findElement(By.id("openvidu-url"));
        urlInput.clear();
        urlInput.sendKeys(OPENVIDU_URL);
        WebElement secretInput = user2.getDriver().findElement(By.id("openvidu-secret"));
        secretInput.clear();
        secretInput.sendKeys(OPENVIDU_SECRET);

        user2.getEventManager().startPolling();

        // Firefox user audio + video
        user2.getDriver().findElement(By.id("add-user-btn")).click();

        // Firefox user video-only
        user2.getDriver().findElement(By.id("add-user-btn")).click();
        user2.getDriver().findElement(By.cssSelector("#openvidu-instance-1 .send-audio-checkbox")).click();

        // Join Firefox users
        user2.getDriver().findElements(By.className("join-btn")).forEach(el -> el.sendKeys(Keys.ENTER));

        try {
            user2.getEventManager().waitUntilEventReaches("connectionCreated", 4);
            user2.getEventManager().waitUntilEventReaches("accessAllowed", 2);
            user2.getEventManager().waitUntilEventReaches("streamCreated", 8);
            user2.getEventManager().waitUntilEventReaches("streamPlaying", 8);

            int nVideos = user2.getDriver().findElements(By.tagName("video")).size();
            Assert.assertEquals("Expected 8 videos in Firefox user but found " + nVideos, 8, nVideos);

            user2.getEventManager().waitUntilEventReaches("recordingStarted", 2);
            user2.getEventManager().waitUntilEventReaches("recordingStopped", 2);

            user2.getEventManager().waitUntilEventReaches("recordingStarted", 4);
            user2.getEventManager().waitUntilEventReaches("recordingStopped", 4);

            user2.getEventManager().waitUntilEventReaches("recordingStarted", 6);
            user2.getEventManager().waitUntilEventReaches("recordingStopped", 6);

            user2.getEventManager().waitUntilEventReaches("recordingStarted", 8);
            user2.getEventManager().waitUntilEventReaches("recordingStopped", 8);

            user2.getEventManager().waitUntilEventReaches("streamDestroyed", 4);
            user2.getEventManager().waitUntilEventReaches("connectionDestroyed", 4);
            user2.getDriver().findElement(By.id("remove-user-btn")).click();
            user2.getDriver().findElement(By.id("remove-user-btn")).click();
            user2.getEventManager().waitUntilEventReaches("sessionDisconnected", 2);
        } catch (Exception e) {
            e.printStackTrace();
            user2.dispose();
            Thread.currentThread().interrupt();
        }
        user2.dispose();
    });
    t.setUncaughtExceptionHandler(h);
    t.start();

    // Chrome user screen share only-video
    user.getDriver().findElement(By.id("add-user-btn")).click();
    user.getDriver().findElement(By.cssSelector("#openvidu-instance-0 .screen-radio")).click();
    user.getDriver().findElement(By.cssSelector("#openvidu-instance-0 .send-audio-checkbox")).click();

    // Chrome user audio-only
    user.getDriver().findElement(By.id("add-user-btn")).click();
    user.getDriver().findElement(By.cssSelector("#openvidu-instance-1 .send-video-checkbox")).click();

    // Join Chrome users
    user.getDriver().findElements(By.className("join-btn")).forEach(el -> el.sendKeys(Keys.ENTER));

    user.getEventManager().waitUntilEventReaches("connectionCreated", 4);
    user.getEventManager().waitUntilEventReaches("accessAllowed", 2);
    user.getEventManager().waitUntilEventReaches("streamCreated", 8);
    user.getEventManager().waitUntilEventReaches("streamPlaying", 8);

    int numberOfVideos = user.getDriver().findElements(By.tagName("video")).size();
    Assert.assertEquals("Expected 8 videos but found " + numberOfVideos, 8, numberOfVideos);

    user.getDriver().findElement(By.id("session-api-btn-0")).click();
    Thread.sleep(1000);
    user.getDriver().findElement(By.id("rec-properties-btn")).click();
    Thread.sleep(500);

    WebElement recordingNameField = user.getDriver().findElement(By.id("recording-name-field"));

    // Video-only COMPOSED recording
    recordingNameField.clear();
    recordingNameField.sendKeys(RECORDING_COMPOSED_VIDEO);
    user.getDriver().findElement(By.id("rec-hasaudio-checkbox")).click();
    Thread.sleep(500);
    user.getDriver().findElement(By.id("start-recording-btn")).click();
    user.getWaiter().until(ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value",
            "Recording started [" + SESSION_NAME + "]"));
    user.getEventManager().waitUntilEventReaches("recordingStarted", 2);

    Thread.sleep(RECORDING_DURATION);

    user.getDriver().findElement(By.id("recording-id-field")).clear();
    user.getDriver().findElement(By.id("recording-id-field")).sendKeys(SESSION_NAME);
    user.getDriver().findElement(By.id("stop-recording-btn")).click();
    user.getWaiter().until(ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value",
            "Recording stopped [" + SESSION_NAME + "]"));
    user.getEventManager().waitUntilEventReaches("recordingStopped", 2);

    // Audio-only COMPOSED recording
    recordingNameField.clear();
    recordingNameField.sendKeys(RECORDING_COMPOSED_AUDIO);
    user.getDriver().findElement(By.id("rec-hasaudio-checkbox")).click();
    user.getDriver().findElement(By.id("rec-hasvideo-checkbox")).click();
    Thread.sleep(500);
    user.getDriver().findElement(By.id("start-recording-btn")).click();
    user.getWaiter().until(ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value",
            "Recording started [" + SESSION_NAME + "-1]"));
    user.getEventManager().waitUntilEventReaches("recordingStarted", 4);

    Thread.sleep(RECORDING_DURATION);

    user.getDriver().findElement(By.id("recording-id-field")).clear();
    user.getDriver().findElement(By.id("recording-id-field")).sendKeys(SESSION_NAME + "-1");
    user.getDriver().findElement(By.id("stop-recording-btn")).click();
    user.getWaiter().until(ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value",
            "Recording stopped [" + SESSION_NAME + "-1]"));
    user.getEventManager().waitUntilEventReaches("recordingStopped", 4);

    // Video-only INDIVIDUAL recording
    recordingNameField.clear();
    recordingNameField.sendKeys(RECORDING_INDIVIDUAL_VIDEO);
    user.getDriver().findElement(By.id("rec-hasaudio-checkbox")).click();
    user.getDriver().findElement(By.id("rec-hasvideo-checkbox")).click();
    Thread.sleep(500);
    user.getDriver().findElement(By.id("rec-outputmode-select")).click();
    Thread.sleep(500);
    user.getDriver().findElement(By.id("option-INDIVIDUAL")).click();
    Thread.sleep(500);
    user.getDriver().findElement(By.id("start-recording-btn")).click();
    user.getWaiter().until(ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value",
            "Recording started [" + SESSION_NAME + "-2]"));
    user.getEventManager().waitUntilEventReaches("recordingStarted", 6);

    Thread.sleep(RECORDING_DURATION);

    user.getDriver().findElement(By.id("recording-id-field")).clear();
    user.getDriver().findElement(By.id("recording-id-field")).sendKeys(SESSION_NAME + "-2");
    user.getDriver().findElement(By.id("stop-recording-btn")).click();
    user.getWaiter().until(ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value",
            "Recording stopped [" + SESSION_NAME + "-2]"));
    user.getEventManager().waitUntilEventReaches("recordingStopped", 6);

    // Audio-only INDIVIDUAL recording
    recordingNameField.clear();
    recordingNameField.sendKeys(RECORDING_INDIVIDUAL_AUDIO);
    user.getDriver().findElement(By.id("rec-hasaudio-checkbox")).click();
    user.getDriver().findElement(By.id("rec-hasvideo-checkbox")).click();
    Thread.sleep(500);
    user.getDriver().findElement(By.id("start-recording-btn")).click();
    user.getWaiter().until(ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value",
            "Recording started [" + SESSION_NAME + "-3]"));
    user.getEventManager().waitUntilEventReaches("recordingStarted", 8);

    Thread.sleep(RECORDING_DURATION);

    user.getDriver().findElement(By.id("recording-id-field")).clear();
    user.getDriver().findElement(By.id("recording-id-field")).sendKeys(SESSION_NAME + "-3");
    user.getDriver().findElement(By.id("stop-recording-btn")).click();
    user.getWaiter().until(ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value",
            "Recording stopped [" + SESSION_NAME + "-3]"));
    user.getEventManager().waitUntilEventReaches("recordingStopped", 8);

    String recordingsPath = "/opt/openvidu/recordings/";

    // Check video-only COMPOSED recording
    String recPath = recordingsPath + SESSION_NAME + "/";
    Recording recording = new OpenVidu(OPENVIDU_URL, OPENVIDU_SECRET).getRecording(SESSION_NAME);
    this.checkMultimediaFile(new File(recPath + recording.getName() + ".mp4"), false, true,
            recording.getDuration(), recording.getResolution(), null, "h264", true);

    // Check audio-only COMPOSED recording
    recPath = recordingsPath + SESSION_NAME + "-1/";
    recording = new OpenVidu(OPENVIDU_URL, OPENVIDU_SECRET).getRecording(SESSION_NAME + "-1");
    this.checkMultimediaFile(new File(recPath + recording.getName() + ".webm"), true, false,
            recording.getDuration(), null, "opus", null, true);

    // Check video-only INDIVIDUAL recording
    recPath = recordingsPath + SESSION_NAME + "-2/";
    recording = new OpenVidu(OPENVIDU_URL, OPENVIDU_SECRET).getRecording(SESSION_NAME + "-2");
    this.checkIndividualRecording(recPath, recording, 3, "opus", "vp8", true);

    // Check audio-only INDIVIDUAL recording
    recPath = recordingsPath + SESSION_NAME + "-3/";
    recording = new OpenVidu(OPENVIDU_URL, OPENVIDU_SECRET).getRecording(SESSION_NAME + "-3");
    this.checkIndividualRecording(recPath, recording, 2, "opus", "vp8", true);

    user.getDriver().findElement(By.id("close-dialog-btn")).click();
    Thread.sleep(500);

    gracefullyLeaveParticipants(2);

    t.join();

    synchronized (lock) {
        if (OpenViduTestAppE2eTest.ex != null) {
            throw OpenViduTestAppE2eTest.ex;
        }
    }
}