function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AbAb 

inserting a record before a null pointer exception is fired

Hello,

i have a use case where i wamt to isert record in to custom log before a null pointer exception is fired.
I am reproducting the null exception and adding log.
but i see that the log is not inserted before the null pointer exception is fired

what can be the issue please
Best Answer chosen by Ab
Mariyan TopalovMariyan Topalov
There's no way to predict when the NPE will be fired.
What you can do is to catch the NPE and insert your Log in the catch block. After that you can rethrow your exception.

Code example:
try{
// code that will produce the Null Pointer Exception
} catch (NullPointerException npe){
//Insert your Log here
//rethrow the NPE 
}

Note that letting null pointer exceptions live in your environment is really, really bad thing. Try to reduce exceptions to 0, as they are considered as "Exceptional behaviour", which means they should not be a part of the normal/regular execution of your code.

All Answers

AbhishekAbhishek (Salesforce Developers) 
https://salesforce.stackexchange.com/questions/80853/custom-exception-handling-logging

This might help you.
Mariyan TopalovMariyan Topalov
There's no way to predict when the NPE will be fired.
What you can do is to catch the NPE and insert your Log in the catch block. After that you can rethrow your exception.

Code example:
try{
// code that will produce the Null Pointer Exception
} catch (NullPointerException npe){
//Insert your Log here
//rethrow the NPE 
}

Note that letting null pointer exceptions live in your environment is really, really bad thing. Try to reduce exceptions to 0, as they are considered as "Exceptional behaviour", which means they should not be a part of the normal/regular execution of your code.
This was selected as the best answer
AbAb
Hello @mariyan
i am following your method, but the inser is not working.
I am able to do it on when i dot rethow execption bu t it is not my usecase