+ Start a Discussion
Ken_KoellnerKen_Koellner 

Can I get a stack trace when I catch an exception (Like I do in the email)

Here's the rub, I want to catch Exceptions but there's always a very small chance a unknown bug gets hit after you release to production and when it does, you want as much info as possible.

 

If I don't catch an Exception, I get an email with something like the following in it--

 

 

caused by: System.NullPointerException: Attempt to de-reference a null object

Class.OpportunityUtils.findBestCustForPid: line 357, column 9
Class.OpportunityUtils.determineCustomerPid: line 189, column 17
Class.OpportunityUtils.determineCustomer: line 70, column 14
Class.DetermineCustomerController.doDetermine: line 38, column 12 External entry point

 That includes a stack trace which pretty much tells me where the problem is.

 

If I catch the exception and code something like--

 

 

	} catch (Exception ex) {
		detRes.status = 'ERROR';
		detRes.message = 'Exception: ' + ex.getTypeName() + ': ' + ex.getMessage() 
		+ ' -- ' + ex.getCause(); 

 

 

Than all I get is --

 

 

21:11:13.429|EXCEPTION_THROWN|[357,9]|System.NullPointerException: Attempt to de-reference a null object

 

 

Is there any way to get the full stack trace from the Exception in the catch?  Then I can print it to debug or maybe put it up on the user's screen.  If I can't see that info, I'm almost better leaving some cases without catch clauses.

 

 

 

bob_buzzardbob_buzzard

According to the system log console docs:

 

EXCEPTION_THROWNLine and character number of the event in the code, exception type, messageApex CodeINFO and above

 

So there's not a way to do this using the standard logging.

 

Stracktrace information only comes out through logging a FATAL_ERROR, but that's not particularly helpful to you.

 

The only other place I can find any references to stack traces is in the metadata api, when running unit tests, so I think you may be out of luck here.

 

 

NaishadhNaishadh

Create your custom exception class and print your exception there.  Hope this help!

 

You can construct exceptions:

  • With no arguments:
     new MyException();
  • With a single String argument that specifies the error message:
     new MyException('This is bad');
  • With a single Exception argument that specifies the cause and that displays in any stack trace:
     new MyException(e);
  • With both a String error message and a chained exception cause that displays in any stack trace:
    new MyException('This is bad', e);An example of a stack trace
Ken_KoellnerKen_Koellner

I don't think that custom exception solution you prospose helps at all.  It still results in an unhandled exception.

 

What I want to to catch the exception and return a friendly error to the user but still capture the stacktrace. 

 

If I re-throw an exception, it will go back to the end-user.

 

twamleytwamley
ex.getStackTraceString();

It works in dev and production. Works with system and custom exceptions. Earliest reference I found was Summer '11.