DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
What's in store for DevOps in 2023? Hear from the experts in our "DZone 2023 Preview: DevOps Edition" on Fri, Jan 27!
Save your seat
  1. DZone
  2. Coding
  3. Java
  4. Getting Stack Trace From an Exception in Java [Snippet]

Getting Stack Trace From an Exception in Java [Snippet]

This code allows you to extract stack trace, providing useful information when you're working on a platform when you can deploy custom Java code.

Shivakumar S user avatar by
Shivakumar S
·
Dec. 01, 17 · Code Snippet
Like (5)
Save
Tweet
Share
9.91K Views

Join the DZone community and get the full member experience.

Join For Free

Extracting stack trace from an exception could provide useful information, especially when you work on platforms where you can deploy custom Java code, exception handling is done by the platform, and exceptions from your custom-written Java code are masked by default platform exceptions.

Typical messages you see from these exceptions logged or shown on the platform UI would be: "Unable to Execute API," "Something went wrong," etc.

Catching these custom exceptions in your own Java code and logging them should be good practice to solve the above-mentioned problem.

public static void extractStackTrace(Exception exception, Logger _logger) {

 StackTraceElement[] elements = exception.getStackTrace();
 String trace = null;
 for (StackTraceElement element: elements) {
  trace = (trace == null) ? element.toString() : trace + ",\n" + element.toString();
 }
 _logger.error(log);
}


This way you have access to stack trace for debugging in the logs, while the platform where you deploy your code does not give you this information.

Java (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Simulate Network Latency and Packet Drop In Linux
  • Spring Cloud: How To Deal With Microservice Configuration (Part 1)
  • Unleashing the Power of JavaScript Modules: A Beginner’s Guide
  • The ChatGPT Model: A Real-Life Example

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: