001 package com.croftsoft.core.lang; 002 003 import java.io.*; 004 005 /********************************************************************* 006 * A collection of static methods to manipulate java.lang.Throwable. 007 * 008 * @version 009 * 2001-05-18 010 * @since 011 * 2001-05-18 012 * @author 013 * <a href="https://www.croftsoft.com/">David W. Croft</a> 014 *********************************************************************/ 015 016 public final class ThrowableLib 017 ////////////////////////////////////////////////////////////////////// 018 ////////////////////////////////////////////////////////////////////// 019 { 020 021 public static void main ( String [ ] args ) 022 ////////////////////////////////////////////////////////////////////// 023 { 024 System.out.println ( getStackTrace ( new RuntimeException ( ) ) ); 025 } 026 027 ////////////////////////////////////////////////////////////////////// 028 ////////////////////////////////////////////////////////////////////// 029 030 /********************************************************************* 031 * Returns the stack trace as a String. 032 *********************************************************************/ 033 public static String getStackTrace ( Throwable throwable ) 034 ////////////////////////////////////////////////////////////////////// 035 { 036 StringWriter stringWriter = new StringWriter ( ); 037 038 PrintWriter printWriter = new PrintWriter ( stringWriter ); 039 040 throwable.printStackTrace ( printWriter ); 041 042 return stringWriter.toString ( ); 043 } 044 045 ////////////////////////////////////////////////////////////////////// 046 ////////////////////////////////////////////////////////////////////// 047 048 private ThrowableLib ( ) { } 049 050 ////////////////////////////////////////////////////////////////////// 051 ////////////////////////////////////////////////////////////////////// 052 }