Skip to content
This repository has been archived by the owner on Apr 3, 2022. It is now read-only.

Commit

Permalink
Tried adding uncaught exception handlers for all windows, hopefully they
Browse files Browse the repository at this point in the history
can catch exceptions that happens outside try blocks.
  • Loading branch information
Stekeblad committed Dec 16, 2019
1 parent 351ce36 commit 52365e7
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 11 deletions.
12 changes: 1 addition & 11 deletions src/main/java/io/github/stekeblad/videouploader/main/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import io.github.stekeblad.videouploader.utils.translation.Translations;
import io.github.stekeblad.videouploader.utils.translation.TranslationsManager;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;

Expand All @@ -33,16 +32,7 @@ public void start(Stage primaryStage) {
Translations trans = TranslationsManager.getTranslation(TranslationBundles.BASE);

// Set the default exception handler, hopefully it can catch some of the exceptions that is not already caught
Thread.setDefaultUncaughtExceptionHandler((thread, exception) -> {
exception.printStackTrace();
if (Platform.isFxApplicationThread()) {
AlertUtils.exceptionDialog(trans.getString("app_name"),
"Sorry, something went wrong!", exception);
} else {
Platform.runLater(() -> AlertUtils.exceptionDialog(trans.getString("app_name"),
"Sorry, something went wrong!", exception));
}
});
Thread.setDefaultUncaughtExceptionHandler((thread, exception) -> AlertUtils.unhandledExceptionDialog(exception));

try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("mainWindow.fxml"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ public class mainWindowController implements IWindowController {
* Initialize things when the window is opened, used instead of initialize as that one does not have access to the scene
*/
public void myInit() {
// Set the default exception handler, hopefully it can catch some of the exceptions that is not already caught
// Is this needed in every window or only in the Main.java file?
Thread.setDefaultUncaughtExceptionHandler((thread, exception) -> AlertUtils.unhandledExceptionDialog(exception));

// Load Translations
transMainWin = TranslationsManager.getTranslation(TranslationBundles.WINDOW_MAIN);
transMainWin.autoTranslate(mainWindowPane);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.stekeblad.videouploader.utils;

import javafx.application.Platform;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.scene.Scene;
Expand Down Expand Up @@ -171,4 +172,29 @@ public static void exceptionDialog(String header, String content, Throwable exce
GridPane pane = makeLongMsgPane(fullContent, false);
paneToWindow(pane, header);
}

/**
* Similar to the exceptionDialog method but used when little is known about the error and the context of where it happened.
* This method prints the stacktrace to the console and attempts to show the exception dialog using a generic
* window header and description text.
*
* @param exception the exception to print to the console and attempt to show in the exceptionDialog
*/
public static void unhandledExceptionDialog(Throwable exception) {
if (exception == null)
return;

exception.printStackTrace();
try {
if (Platform.isFxApplicationThread()) {
exceptionDialog("Stekeblads Video Uploader",
"Sorry, something went wrong!", exception);
} else {
Platform.runLater(() -> exceptionDialog("Stekeblads Video Uploader",
"Sorry, something went wrong!", exception));
}
} catch (Exception e) {
// not much to do here, we tried showing the exception dialog but we failed
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public class LocalizeCategoriesWindowController implements IWindowController {
* Initialize things when the window is opened, used instead of initialize as that one does not have access to the scene
*/
public void myInit() {
// Set the default exception handler, hopefully it can catch some of the exceptions that is not already caught
Thread.setDefaultUncaughtExceptionHandler((thread, exception) -> AlertUtils.unhandledExceptionDialog(exception));

// Filter what can be entered into the textFields
txt_country.textProperty().addListener((observable, oldValue, newValue) -> {
if(! newValue.matches("[A-Za-z]*") || newValue.length() > 2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public class ManagePlaylistsWindowController implements IWindowController {
* Initialize a few things when the window is opened, used instead of initialize as that one does not have access to the scene
*/
public void myInit() {
// Set the default exception handler, hopefully it can catch some of the exceptions that is not already caught
Thread.setDefaultUncaughtExceptionHandler((thread, exception) -> AlertUtils.unhandledExceptionDialog(exception));

// Load Translations
transBasic = TranslationsManager.getTranslation(TranslationBundles.BASE);
transPlaylistWindow = TranslationsManager.getTranslation(TranslationBundles.WINDOW_PLAYLIST);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public class MetaDataToolWindowController implements IWindowController {

@Override
public void myInit() {
// Set the default exception handler, hopefully it can catch some of the exceptions that is not already caught
Thread.setDefaultUncaughtExceptionHandler((thread, exception) -> AlertUtils.unhandledExceptionDialog(exception));

prog_readingFile.setProgress(ProgressIndicator.INDETERMINATE_PROGRESS);
prog_readingFile.setVisible(false);
trans_metaDataTool = TranslationsManager.getTranslation(TranslationBundles.WINDOW_TOOL_META);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ public class PresetsWindowController {
* Initialize a few things when the window is opened, used instead of initialize as that one does not have access to the scene
*/
public void myInit(boolean disableLocaleChange) {
// Set the default exception handler, hopefully it can catch some of the exceptions that is not already caught
Thread.setDefaultUncaughtExceptionHandler((thread, exception) -> AlertUtils.unhandledExceptionDialog(exception));

configManager = ConfigManager.INSTANCE;
categoryUtils = CategoryUtils.INSTANCE;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public class SettingsWindowController implements IWindowController {
* Initialize a few things when the window is opened, used instead of initialize as that one does not have access to the scene
*/
public void myInit() {
// Set the default exception handler, hopefully it can catch some of the exceptions that is not already caught
Thread.setDefaultUncaughtExceptionHandler((thread, exception) -> AlertUtils.unhandledExceptionDialog(exception));

configManager = ConfigManager.INSTANCE;
translationsMeta = new TranslationsMeta();
basicTrans = TranslationsManager.getTranslation(TranslationBundles.BASE);
Expand Down

0 comments on commit 52365e7

Please sign in to comment.