Questions tagged [try-catch]

try-catch is used to catch the exception.

Filter by
Sorted by
Tagged with
70
votes
5answers
9k views

Pokemon Catch - Generic Exception Handling In Apex

I read a great post about this topic on Stack Overflow: Is it really that bad to catch a general exception? However, I asked this question on our exchange because I am interested in a more specific ...
14
votes
5answers
6k 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? ...
8
votes
1answer
4k views

Catch Regex too complicated when splitting a string in salesforce

I have created a function wherein I split a string from the csv filecsvAsString.split('\n') . It is working when the string is not so big. But when I try to split a very large string, an error will ...
14
votes
3answers
6k views

Can I throw an exception in apex and still log the caught exception?

I'm really wrestling with the best error handling strategy. My goal is to handle all exceptions that I can identify, log them to a table in the database, and surface a helpful message about what ...
5
votes
2answers
6k views

Handling exception using custom exception

I have an custom exception , just an example to see how I can write my own exception class and what could be the purpose of the exception. public class AccountException extends Exception { ...
4
votes
1answer
3k views

Custom Exception Handling & Logging

I've set out to build a custom error handling and logging feature for my org wherein all apex exceptions will be logged into a custom sObject for analysis and follow-up. public class TriggerException ...
2
votes
3answers
121 views

Coverage for Catch Block (cant generate exception in try{}) give another solution

NOTE : This question is not duplicate.Please go through code once. Here is the situation - I am calling this method from my test class, and here My_obj__c records are getting created and added in ...
10
votes
1answer
4k views

Catch System.AssertException

Is there a way to catch System.AssertException? For example, some class is known to throw assertions in certain cases and it's ok to deal with that on some upper level. But unfortunately ...
3
votes
0answers
34 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 ...
-1
votes
1answer
95 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 ...