Questions tagged [try-catch]

try-catch is used to catch the exception.

Filter by
Sorted by
Tagged with
0 votes
0 answers
23 views

DML Exception not being caught

ClassA { Method1() { Method2(); } Method2() { ClassB.Method3(); } } ClassB { Method3() { //Failing DML Operation } } How come I can only ...
user avatar
  • 1
-1 votes
1 answer
37 views

try catch block not catching limit exception after query

I have a SOQL query call inside a try catch block, because I want to catch if a LimitException is thrown, but for some reason, when the exception is thrown after the query,the aura handled exception ...
user avatar
-2 votes
1 answer
82 views

cover return statement in test class?

can someone help with test class covering in a try catch block? the first methods maybe is not necessary but anyway, is the same issue with all methods. help pls! (65% covered) Class Controller public ...
user avatar
0 votes
2 answers
122 views

Code Coverage for Catch block in test class?

I'm unable to cover the code coverage for catch block.Kindly help me to cover that code lines as well. Apex Class: public without sharing class ZTS_EU_Contact_populateCallDates { Map <id, ...
user avatar
0 votes
0 answers
25 views

Test class try catch on rest callout [duplicate]

So i am making post request and catch error by checking status code and by try catch too. And how can i test try catch block? I think mock response is useless here. So requst looks like: public void ...
user avatar
-2 votes
1 answer
39 views

I want to create a task whenever I add or modify an opportunity product

trigger OpptyLineItem on OpportunityLineItem (after insert, after update) { if(Trigger.isAfter) { if(Trigger.isInsert && Trigger.isUpdate) { for(...
user avatar
  • 31
1 vote
1 answer
53 views

Do not want to keep specific type of exception with try catch block

I have the following catch block: } catch(Exception ex) { Database.rollback(sp); RestContext.response.statuscode = (ex.getTypeName() == 'mainClass.DuplicateException') ? 409 : 400; ...
user avatar
0 votes
1 answer
129 views

Catch error in scheduled batch

I want to catch here an error on deleting records. but i dont really understand how? Can u help me please? public with sharing class AccountTest implements Database.Batchable<SObject> { ...
user avatar
0 votes
1 answer
559 views

How to cover a catch statement in a test class?

I have a simple after insert trigger which inserts a task when an account is created. I am struggling to cover the catch statement in my test class. Is there a way to cover it? Apex Trigger trigger ...
user avatar
0 votes
1 answer
39 views

try/catch vs describe calls inside for loop which has more performance impact?

We have a generic method which is called in a for loop for each record where we try to get a value of a field using Sobject get method. Before getting the field we have to make sure that field ...
user avatar
  • 125
-1 votes
1 answer
86 views

"Variable does not exist" error in catch block even when the variable is outside the try block

I have written a batch class and using try-catch in its execute method. But on saving the class, getting variable does not exist error. Sample code is given below: global void execute(Database....
user avatar
0 votes
1 answer
2k views

Try and Catch in apex

I am using one try-catch block inside which there are multiple SOQL queries WITH SECURITY_ENFORCED. Do I need try catch block separately for each SOQL or the query exception would be handled in that ...
user avatar
  • 111
0 votes
1 answer
177 views

Try Catch - Exception

I have conceptual question here for try catch. We use try catch so that exception can be skipped and further code gets executed (*correct me if i am wrong) Point is Exeption is kind of error which ...
user avatar
  • 21
0 votes
1 answer
1k views

How to advoid Maximum view state size limit (170KB) exceeded. Actual view state size for this page was 292.001KB

I currently have an apex class which creates an object record and attaches 2 files. I added a loop to be able to have the option to add more attachments based on counter. I currently have some ...
user avatar
  • 2,092
-2 votes
1 answer
1k views

try catch with addError not showing message

I wrote this code in a trigger so addError works fine, but when code fails within a try-catch block i expected my addError message but i getting a error as i not using the try-catch. public with ...
user avatar
0 votes
1 answer
206 views

How to cover the catch block for after insert Trigger?

When you create a Barrel, a Filling is also created. Is there any chance to make a fake error in unit test between Barrel and Filling to crush the upsert process and cover the catch block? Helper: ...
user avatar
  • 45
3 votes
0 answers
88 views

Best approach to handling Post-Install script exception

As inspired by this post on when to use try catch blocks, if we have a post-install script for our managed package, what's the best approach to gracefully catching and handling an exception? It ...
user avatar
  • 4,702
18 votes
5 answers
9k views

When do we really need try/catch?

I know about 'always in DML operations'. But for what? What do we get? The code will crash anyway, and we can find the error in logs even without this construction. What's the main point of try/catch? ...
user avatar
4 votes
0 answers
62 views

Is this a good Model for "Guaranteeing" Record Updates Don't Fail due to ROW_LOCK/Race Conditions?

Background: The zqu__Quote__c object in our org has two child objects from another managed package we use for eSignature and credit card capture. One of those two child objects collects the Credit ...
user avatar
4 votes
1 answer
840 views

Is it Possible to Catch an Unknown Exception?

I am creating a button that will allow System Admins to reset user passwords via System.resetPassword. We have password restrictions setup such that a password must exist for 24 hours. Therefore, if ...
user avatar
0 votes
1 answer
31 views

Start fresh after error handling message

So I have a validation rule that doesn't allow a user to enter in a "Receiving Quantity" that is greater than the "Open Quantity" custom field. If I have an open quantity of 5, but I enter in 6 as the ...
user avatar
  • 47
1 vote
3 answers
864 views

How to cover Catch block in ApexUnitTest class when using DML statement in try body

I have some apex code, where I try to upsert a list of records: try { upsert recordList Accounts; } catch (DmlException e) { //do a loooot of stuff in order to provide a proper ...
user avatar
0 votes
1 answer
2k views

Need help getting value to refresh in LWC without refreshing browser

I have an apex class that passes in the value of 'lastCallSource' to do a SOQL query on a Custom MetaData Type that will display a custom field value from the metaData to the user. I have this working,...
user avatar
  • 1,278
0 votes
1 answer
601 views

Why is SSJS Platform.Function.ParseJSON not going into catch block if JSON has bad format

I expect this code going into catch block because "hello" is no valid json. But this is not the case. Do you have any idea? <script runat="server"> Platform.Load("core","1.1.1"); var ...
user avatar
  • 61
0 votes
2 answers
445 views

Why catch code of an try catch exception are not executed when System.LimitException: Too many DML statements: 151

I have the next code block that generates a limitException: Account myAccount = new Account(Name = 'MyAccount'); Insert myAccount; For (Integer x = 0; x < 150; x++) {Account newAccount = new ...
user avatar
3 votes
1 answer
3k views

SFMC SSJS error handling - try catch

I have the following code: <script runat="server"> Platform.Load("Core","1.1.1"); try{ var logDE = DataExtension.Init("testDE"); throw "this is an error"; logDE.Rows.Add({"c1":"...
user avatar
  • 31
3 votes
2 answers
4k views

Salesforce - soql System.QueryException Try Catch Issues

I am building dynamic soql query from an object. I am getting the object in a Field and another field from a object in Salesforce for the Query. With that said I am trying to capture the Query ...
user avatar
0 votes
1 answer
825 views

Multiple DML operation in the same try and catch block

I wanted to ask if there a problem doing upsert and delete in the same try-catch block, or maybe it better to separate them. for example : try { Schema.SObjectField oCode = Opportunity.Fields....
user avatar
  • 963
-1 votes
1 answer
307 views

Unable to cover try catch for UserPackageLicense test class

I have below UserPackageLicense class and test class.I am unable to cover the test class try catch block Class:- public class AssignPackageLicense { public static boolean flag=true; static String ...
user avatar
  • 105
0 votes
2 answers
662 views

Catch block code coverage for @HttpPost and @HttpGet REST API

I have some code in my catch block for both @Post and @Get methods. I need to cover catch block to get to 75% for GET, got catch block covered for POST Here is an example : // POST my JSON : [ ...
user avatar
  • 585
1 vote
2 answers
675 views

Exception not being caught; System.FinalException: Cannot modify a collection while it is being iterated

Why is this Exception not being caught? I read that System.LimitException is not caught but didn't see anywhere about System.FinalException. Non-Working Code @AuraEnabled public static List<...
user avatar
  • 545
10 votes
4 answers
3k views

Using try, catch and finally in test class (dreamhouseapp)

In the test classes of the DreamHouse Sample app all the functions have the following structure: static testMethod void testSomething() { Boolean success = true; try { ... } catch (...
user avatar
  • 117
0 votes
1 answer
36 views

How to catch update exception and what kind of notification to do here?

I have a trigger with helper class and test. But I don't know how to test update there and what kind of notification I need. Thanks for any help. Trigger: trigger ArchivedTrigger on Item__c (after ...
user avatar
  • 133
5 votes
2 answers
904 views

Custom Logging, @Future, Transaction Control

I have a similar question to these two: Custom Logging and Future Methods Custom Exception Handling & Logging But my question this time is: can I put the @Future annotation on the Logger Class? ...
user avatar
  • 3,179
9 votes
1 answer
1k views

Handing REST API POST Exceptions using SSJS Try-Catch on a Landing Page Form Submission with AMPSCRIPT

I have an issue with a landing page containing a webform that submits input from the user and using AMPSCRIPT I make a Rest API call using the HTTPPost2 function to utilize the validateEmail resource. ...
user avatar
5 votes
1 answer
2k views

Exceptions and DML

It was to my understanding that when you throw an exception all previous operations are rolled back. When an exception occurs, code execution halts and any DML operations that were processed ...
user avatar
  • 3,179
1 vote
2 answers
1k views

Governor Limit Exception and Finally blocks

We all know that if you hit a Governor Limit Exception, your code/application will terminate with an unhandleable exception know as a Governor Limit Exception. My question is does your code ...
user avatar
  • 703
0 votes
3 answers
3k views

How to write a try catch for Rest Api

How do I catch an error if the insert goes wrong in my rest API or is this not needed since I get an API respone? @HttpPost global static ID createInvoice ( String customerId, ...
user avatar
  • 2,092
-1 votes
2 answers
8k views

Not able to cover catch block in test class

I have below batch class which having finish method global void finish(Database.BatchableContext batchableContext){ Savepoint sp = Database.setSavepoint(); try{ if(ErrorCount > 0)...
user avatar
  • 9
0 votes
3 answers
1k views

how to cover code for catch block?

public class QMT_ReviewSubmit { public static QMT_Apex_Debug_Logs__c logger = new QMT_Apex_Debug_Logs__c(Apex_Class__c = 'QMT_ReviewSubmit', Type__c = 'UI'); public static String QMTThumbnail = ...
user avatar
  • 383
4 votes
1 answer
539 views

DMLException handling in a Before trigger context?

I got 99% of the way through refactoring a trigger from "after" to "before" when I noticed one little thing I'd overlooked when judging whether or not to do it: My trigger copies data from "holding ...
user avatar
  • 1,139
13 votes
2 answers
4k views

What is an elegant way to catch multiple exceptions without catching generic exceptions?

I don't want to catch generic exceptions, but I want to handle all the exceptions I want to catch exactly the same way. For arguments sake, lets say the code is: try { doSomething(); } catch(...
user avatar
-2 votes
1 answer
1k views

How can exceptions be handled in Apex without using try/catch? [closed]

How can exceptions be handled in Apex without using try/catch( / finally)?
user avatar
0 votes
1 answer
919 views

Test Code coverage to Cover Catch Block

My Apex Class where I'm having a validation rule for duplicate name check in Catch block public pagereference save(){ try{ upsert complexity; pg = new ApexPages....
user avatar
  • 1,095
0 votes
2 answers
2k views

Try/Catch in for loop

I am trying to grab a list of unique users from a subset of leads and I am running into a System.QueryException: List has no rows for assignment to SObject error. Here is my new code below. If I get ...
user avatar
0 votes
2 answers
625 views

Try Catch blocks within a Custom Controller?

I have a VF page which is fired based on a custom button click from Case record's detail page. window.location = 'apex/onClickPage?Id={!Case.Id}'; In Apex controller class, I am getting the Id from ...
user avatar
  • 1,490
1 vote
1 answer
6k views

How to do code coverage for Catch Block:

Below is my apex class: public void submitForApproval(List<Account> acc) { try{ for(SObject ac :acc) { if((ac.Account_Record_Type_Name__c == 'A' || ac....
user avatar
1 vote
2 answers
1k views

Test Class for Bounced Email (try/catch coverage)

I'm trying to write a test class to cover the try/catch section in the following code but am unsure how to test for the error. I only have 69% code coverage w/o it. Or is there a way in the test ...
user avatar
  • 179
0 votes
1 answer
475 views

error message show if cityname is already exist on vfpage

want to show error message that if cityname is duplicate then show error message cityname already exists! whats i'm doing wrong anyone please tell When saving record from cities tab and if dupicate ...
user avatar
  • 1,298
2 votes
1 answer
64 views

Try Catch Only Reading First Line

I am writing a try catch in my constructor but can not get past the first query of crs. Is there a limit on how many queries you can do in a try catch? What would the reason be for not allowing the ...
user avatar
  • 3,179