Commit 062f2b34 authored by Bhargavi Ghanta's avatar Bhargavi Ghanta

Initial commit: GlobalExceptionHandler added task 7 completed

parents
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
\ No newline at end of file
# Default ignored files
/shelf/
/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="ms-17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/GlobalExceptionHandler.iml" filepath="$PROJECT_DIR$/GlobalExceptionHandler.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class GlobalExceptionHandler {
public static void main(String[] args) {
// Set global exception handler
Thread.setDefaultUncaughtExceptionHandler(new MyExceptionHandler());
// Simulate an unhandled exception
System.out.println("Program started...");
String value = null;
System.out.println(value.length()); // This will throw NullPointerException
}
// Custom global exception handler
static class MyExceptionHandler implements Thread.UncaughtExceptionHandler {
@Override
public void uncaughtException(Thread t, Throwable e) {
try (PrintWriter writer = new PrintWriter(new FileWriter("error_log.txt", true))) {
writer.println("=== Uncaught Exception in thread \"" + t.getName() + "\" ===");
writer.println("Exception: " + e);
for (StackTraceElement element : e.getStackTrace()) {
writer.println("\tat " + element);
}
writer.println(); // blank line between exceptions
} catch (IOException ioException) {
System.err.println("Failed to write to log file: " + ioException.getMessage());
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment