I have multiple stack traces and I want to generate unique id for each of them using the Throwable methods like getStackTrace, getCause etc. The problem I am facing is that these stack traces are already generated and I am fetching them from a db where they have been stored as strings. Is there any way I could convert them back to Throwable?
- 
 2Don't they have some id (primary key) already associated with them if they are stored in a db? And no, I don't think it can be done and why would you need to.Joakim Danielson– Joakim Danielson2020年06月18日 07:35:46 +00:00Commented Jun 18, 2020 at 7:35
 - 
 Can you elaborate a bit more what are you trying to achieve? How would you generate a unique id for Throwable if you would have it? Why do you want to create IDs for them? Would help to answer the question better if we understand what is it that you try to achieve by creating IDs.Raipe– Raipe2020年06月18日 07:36:24 +00:00Commented Jun 18, 2020 at 7:36
 - 
 I want to group the stack traces uniquely. It happens that there are multiple stack traces that mostly vary due to differences in os or app version but essentially they are the same. This causes problem as there maybe tens of thousands of crash reports, but only a dozen of them may be unique.Akshat Choudhary– Akshat Choudhary2020年06月18日 07:43:48 +00:00Commented Jun 18, 2020 at 7:43
 - 
 Based on your comment (which gives important additional context which you should have included in the question's description from the start) I think it'd be better to try to solve this problem on the database (SQL) itself rather than fetching the data and processing it in Java. Why is solving your problem on the database not an option?Ivo Mori– Ivo Mori2020年06月18日 09:22:10 +00:00Commented Jun 18, 2020 at 9:22
 - 
 Possibly Find sql records containing similar strings, Find rows with similar string values or similar may be of interest.Ivo Mori– Ivo Mori2020年06月18日 09:29:58 +00:00Commented Jun 18, 2020 at 9:29
 
2 Answers 2
Exception implements Serializable. If you store your exceptions into the database using Java's serialization API, you should be able to deserialize them back into Exception objects using that same API.
See https://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html for the official documentation on this.
If you do not currently store them through that mechanism, you are out of luck and will need to change that.
Comments
You can create exceptions with stacktraces manually from a text-only stack trace if you are a little creative, but it obviously won't be identical to the original exception.
Here's an example stacktrace from a log:
org.apache.solr.common.SolrException: org.apache.solr.client.solrj.SolrServerException: cancel_stream_error/closing
 at org.apache.solr.handler.component.SearchHandler.throwSolrException(SearchHandler.java:665)
 at org.apache.solr.handler.component.SearchHandler.handleRequestBody(SearchHandler.java:583)
 at org.apache.solr.handler.RequestHandlerBase.handleRequest(RequestHandlerBase.java:226)
 at org.apache.solr.core.SolrCore.execute(SolrCore.java:2884)
Running ex.printStackTrace() will print the exact same thing from this hand crafted exception:
SolrException ex = new SolrException(
 SolrException.ErrorCode.SERVER_ERROR, 
 "org.apache.solr.client.solrj.SolrServerException: cancel_stream_error/closing"
);
ex.setStackTrace(new StackTraceElement[]{
 new StackTraceElement("org.apache.solr.handler.component.SearchHandler", "throwSolrException", "SearchHandler.java", 665),
 new StackTraceElement("org.apache.solr.handler.component.SearchHandler", "handleRequestBody", "SearchHandler.java", 583),
 new StackTraceElement("org.apache.solr.handler.RequestHandlerBase", "handleRequest", "RequestHandlerBase.java", 226),
 new StackTraceElement("org.apache.solr.core.SolrCore", "execute", "SolrCore.java", 2884),
});