Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
ascendbatch2025
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Bhargavi Ghanta
ascendbatch2025
Commits
6be803f2
Commit
6be803f2
authored
Jun 11, 2025
by
Bhargavi Ghanta
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Initial commit
parent
062f2b34
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
40 additions
and
35 deletions
+40
-35
GlobalExceptionHandler.java
src/GlobalExceptionHandler.java
+0
-35
GlobalExceptionLogger.java
src/GlobalExceptionLogger.java
+40
-0
No files found.
src/GlobalExceptionHandler.java
deleted
100644 → 0
View file @
062f2b34
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
());
}
}
}
}
src/GlobalExceptionLogger.java
0 → 100644
View file @
6be803f2
import
java.io.FileWriter
;
import
java.io.IOException
;
import
java.io.PrintWriter
;
public
class
GlobalExceptionLogger
{
public
static
void
main
(
String
[]
args
)
{
// Set up global exception handler
Thread
.
setDefaultUncaughtExceptionHandler
((
thread
,
throwable
)
->
{
logException
(
throwable
);
});
// Sample code that throws an unhandled exception
System
.
out
.
println
(
"Program started."
);
int
result
=
divide
(
10
,
0
);
// This will cause ArithmeticException
System
.
out
.
println
(
"Result: "
+
result
);
}
// Method that may throw an exception
public
static
int
divide
(
int
a
,
int
b
)
{
return
a
/
b
;
// division by zero triggers an exception
}
// Method to log exceptions to a file
public
static
void
logException
(
Throwable
throwable
)
{
try
(
PrintWriter
writer
=
new
PrintWriter
(
new
FileWriter
(
"error.log"
,
true
)))
{
writer
.
println
(
"----- Exception Occurred -----"
);
writer
.
println
(
"Thread: "
+
Thread
.
currentThread
().
getName
());
writer
.
println
(
"Exception: "
+
throwable
);
for
(
StackTraceElement
element
:
throwable
.
getStackTrace
())
{
writer
.
println
(
"\tat "
+
element
);
}
writer
.
println
();
// Blank line for separation
}
catch
(
IOException
e
)
{
System
.
err
.
println
(
"Failed to log exception: "
+
e
.
getMessage
());
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment