Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.

handling exception in custom components

Avatar

Level 6
Hello,



Is there any info about handling exceptions in custom components? I would like to expose some exceptions to the Livecycle process.



Thank you
2 Replies

Avatar

Level 10
Unfortunately there is not a lot of documentation on that area.



This is a quick example I built a while ago. The list of exceptions the function can throw which are JasException (which is a custom exception), and FileNotFoundException are what you're going to see in the list from the lightning bolt.



Jasmin



public void ReadFile(String code) throws JasException,FileNotFoundException{



System.out.println("!!!******** Init Compontent with value " + code);

code = "3";



if(code.equalsIgnoreCase("1")){

System.out.println("***** In code 1");

throw new JasException();

}



try{

if(code.equalsIgnoreCase("2")){

System.out.println("***** In code 2");

FileInputStream fstream = new FileInputStream("textfile.txt");

}

}

catch (Exception e){

throw new FileNotFoundException();

}



if(code.equalsIgnoreCase("3")){

int intCode = 0;

intCode = intCode /0;

}



System.out.println("******** End Compontent");

}

Avatar

Former Community Member
I have faced this situation for a while and found solution.



1.Create your custom Exception say (custom exception)

2. Set the properties like errorcode,errormessage, and throwable (if u want to bubble up the exceptions).

3. Create a custom object to hold the results (i.e like a java bean)

4. In your custom component methods just return the custom object and set exception as a property.



for e.g.



public class CustomReturn {



private String result;

private CustomException exception;



//getters & setters

}



public class CustomException {



private int errorCode = -1;

private String errorMessage;

private Throwable error;



//getters & setters

}



public class CustomClass {



public CustomReturn testValue() {

CustomReturn returnValue = new CustomReturn();



try {

//perform operations

}

catch(Exception ex) {

CustomException cex = new CustomException ();

cex .setErrorCode(1000);

cex .setErrorMessage(ex.getMessage());

cex .setErr(ex);

returnValue .setException(cex);

}



return returnValue;

}

}



return returnValue;



As you see , you can check for the exception in your return type using the bean property.



This will overcome the limitation of not able to retrieve the error stack incase of errors with default livecycle components.



Hope this helps.



-Senthil