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
Hitesh AlgamwarHitesh Algamwar 

how we can access the variable which is created in try block and we need to use than in the catch block.

for example :- 

class A{

try {
string a = abc ;
}catch(exception e)
{
// now here i want to create one object record 

 object b = new object ();
b.stringval = now here i need to update the value which in string a in try block.
}

}
Best Answer chosen by Hitesh Algamwar
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Hitesh,

No you cannot access the variable which is in try block in catch block. You can try somethiing like this.

If you define the variable outside the try block so you can use the variable any where 
class A{
string a;
try {
a = 'abc' ;
}catch(exception e)
{
// now here i want to create one object record 

 object b = new object ();
b.stringval = now here i need to update the value which in string a in try block.
}

}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Hitesh,

No you cannot access the variable which is in try block in catch block. You can try somethiing like this.

If you define the variable outside the try block so you can use the variable any where 
class A{
string a;
try {
a = 'abc' ;
}catch(exception e)
{
// now here i want to create one object record 

 object b = new object ();
b.stringval = now here i need to update the value which in string a in try block.
}

}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
This was selected as the best answer
Hitesh AlgamwarHitesh Algamwar
Thanks  Sai Praveen!!! I have just tried the same and its working thanks.