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
chandu kumarchandu kumar 

exception

public class myException extends exception {}  
over next public class controller
{
try{ void myexception () ] catch( exception e); catch(myexception me);
}over
question is ... which exception fires?
Amit Chaudhary 8Amit Chaudhary 8
Hi Chandu,

If you will write your code like below then standard exception class will call every time time. As Excecption class is parent class.

try{
..................
}
catch( exception e)
{.....
}
catch(myexception me)
{.....
}

If you want myException will call then code should should in below order

try{
..................
}
catch(myexception me)
{.....
}
catch( exception e)
{.....
}

Check below post for more info
https://developer.salesforce.com/docs/atlas.en-us.apex_workbook.meta/apex_workbook/apex7_5.htm
https://developer.salesforce.com/page/An_Introduction_to_Exception_Handling

Let us know if this will help you.

 
Mahesh DMahesh D
Hi ,

We can't write like this 
 
try{
..................
}
catch( exception e)
{.....
}
catch(myexception me)
{.....
}

because if the first Catch block is catching the Exception which is a parent and generic one, it will not allow any other exceptions after that and it will throw compile time error.

Hence you have to write like below:
 
try{
..................
}
catch(myexception me)
{.....
}
catch( exception e)
{.....
}
Another example:
 
try{
     //Your code here
} catch (ListException e) {
     //Optional catch of a specific exception type
     //Specific exception handling code here
} catch (Exception e) {
     //Generic exception handling code here
} finally {
     //optional finally block
     //code to run whether there is an exception or not
}

How do I use custom exceptions?
You may decide that you want to interrupt your program flow even when a system exception does not occur. In that case you can throw your own custom exception. It's very easy to do - simply create a new exception class by extending the Exception class. Here's an example of a custom exception, and some code that throws the exception:
 
//define your custom exception
public class MyException extends Exception{}

//try, throw and catch
try {
     Integer i;
     //Your code here
     if ( i < 5 ) throw new MyException();
} catch ( MyException e ) {
     //Your MyException handling code here
}

Please find the below useful information on Exception Handling in Apex.

What is an exception?
An exception is a special condition that changes the normal flow of program execution. That is, it's when something bad happens that the program can't deal with during execution. Exceptions are the language's way of throwing up its hands and saying, "I can't deal with this, you need to."
So what kinds of conditions can cause Apex to raise, or throw, an exception? Here are the most common examples:
(1) Your code expects a value from something that is currently null
(2) An insert or update statement fails to pass a custom validation rule you have set
(3) Assigning a query that returns no records or more than one record to a singleton sObject variable
(4) Accessing a list index that is out of bounds
In all these instances we're trying something that the language deems impossible, and an exception is thrown. Apex has 20 different kinds of exceptions--that's a lot of different kinds of exceptions, but since they're all subclassed from a generic exception class they are very similar to deal with. All the exceptions support standard methods for accessing the error message and the exception type.

Also go througth the below links:

https://developer.salesforce.com/page/An_Introduction_to_Exception_Handling

https://developer.salesforce.com/docs/atlas.en-us.apex_workbook.meta/apex_workbook/apex7_2.htm

https://developer.salesforce.com/docs/atlas.en-us.apex_workbook.meta/apex_workbook/apex7_intro.htm

https://developer.salesforce.com/docs/atlas.en-us.apex_workbook.meta/apex_workbook/apex7_4.htm

http://sfdcsrini.blogspot.com/2014/04/what-is-exception-handling-in-apex.html


Please do let me know if it is useful for you.

Regards,
Mahesh