Programmatically Trigger Exceptions for excel to pdf convertion

cc @amjad.sahi: I tag you in this since you were very helpful in our last post and I appreciated your kind invitation to reach out again.

Hello dear forum,

# The issue:
I am having difficulties triggering aspose.cell Exceptions programmatically for a unit test.

# Context:
I’m working on adding test coverage for the PdfSaveOptions.setIgnoreError(true) feature to ensure unhandled errors during conversion are ignored, and the rest of the document renders as expected.

However, despite consulting the ExceptionType reference, I cannot programmatically trigger exceptions in my test cases, even with setIgnoreError(false) or omitting the setting entirely. While Excel raises error pop-ups when opening the generated files, no exceptions are thrown during conversion.

# Code Summary: Test Case: (Attempting to trigger exceptions like CHART, DATA_TYPE, etc.)

Workbook workbook = new Workbook();
Worksheet sheet = workbook.getWorksheets().get(0);
Cells cells = workbook.getWorksheets().get(0).getCells();

// Unsupported Chart Type (ExceptionType.CHART)
int chartIndexes = sheet.getCharts().add(ChartType.BUBBLE, 1, 1, 10, 10);
Chart chart = sheet.getCharts().get(chartIndexes);
chart.getNSeries().add("INVALID_SERIES", true); 

// Invalid Data Type (ExceptionType.DATA_TYPE)
cells.get("B1").putValue(Double.POSITIVE_INFINITY); 

// Invalid Data Validation (DATA_VALIDATION)
Validation validation = sheet.getValidations().get(sheet.getValidations().add());
validation.setType(ValidationType.DECIMAL);
validation.setFormula1("INVALID_FORMULA"); 

// Invalid Page Setup (PAGE_SETUP)
PageSetup pageSetup = sheet.getPageSetup();
pageSetup.setPaperSize(1234); 

// Unsupported Shape (SHAPE)
Shape unsupportedShape = sheet.getShapes().addShape(MsoDrawingType.SMART_ART, 0, 0, 0, 0, 100, 100);
unsupportedShape.setName("UnsupportedShape");

// Invalid PivotTable (PIVOT_TABLE)
int pivotTableIndexes = sheet.getPivotTables().add("A1:E10", "G1", "InvalidPivot");
PivotTable pivotTable = sheet.getPivotTables().get(pivotTableIndexes);
pivotTable.addFieldToArea(PivotFieldType.ROW, 0); 
pivotTable.setAutoFormatType(-1); 

// Conversion
Path pdfPath = saveWorkbookToPdf(workbook);

Code Under Test: I’ve attached the code I’m testing. I’ve also checked if exceptions are suppressed, but the handling seems straightforward.
convertExcelToPDF method.zip (1.9 KB)

Observations:

  • Files generated do produce Excel pop-ups when opened, indicating errors exist.
  • No exception (e.g., CellsException) is triggered during workbook.save(...), even with PdfSaveOptions.setIgnoreError(false).

# Request for Guidance:

  1. Am I correctly attempting to trigger exceptions for PdfSaveOptions.setIgnoreError(false)?
  2. Are there specific conditions or scenarios needed to observe exceptions for these cases?
  3. Could anything in the code under test be inadvertently suppressing exceptions?

Thank you in advance for your time and expertise! Looking forward to your insights.

Best regards,

Ioan

@l.alex,

Thanks for the sample code snippets and details.

It seems you are trying to trigger specific exceptions in Aspose.Cells and also to test the behavior of PdfSaveOptions.setIgnoreError(false) versus true. However, Aspose.Cells may not throw runtime exceptions for these situations in a way that you expect. Aspose.Cells is designed to handle many Excel-related errors, but it may not throw exceptions during operations that could create invalid content, unlike Excel which displays pop-ups for such issues. Instead, the component might try to continue processing and leave errors in the resulting document without halting execution on a safer side. That’s why you’re seeing pop-ups when opening files in the generated Excel file in MS Excel but no exceptions during the workbook.save() method.

The setIgnoreError option is specific to errors that might occur during the rendering to PDF. It is not about catching general data input errors but rather if the rendering process encounters data it can’t handle. Setting IgnoreError to false should force the PDF save process to raise exceptions when a rendering issue is encountered.

Anyways, we will evaluate your scenario/case in details. We will give get back to you with more details and insights on your requirements and issues.

Thank you @amjad.sahi for your detailed response. I understand that many of the exceptions I attempted to generate were related to loading or processing the Excel file rather than issues encountered during rendering to PDF.

Since I am looking to specifically trigger exceptions during the PDF conversion process for scenarios where setIgnoreError(false) should raise an exception due to rendering errors, I would appreciate any suggestions or ideas for:

  • Generating errors that are specific to the rendering/conversion to PDF stage
  • Scenarios that are likely to cause rendering failures (e.g., unsupported features, corrupt rendering objects)
  • Any limitations or nuances in the setIgnoreError feature I should be aware of

My goal is to create test cases that ensure setIgnoreError(false) correctly stops processing when an error occurs, while setIgnoreError(true) gracefully continues rendering.

Thank you for your time and expertise!

Best regards,
Ioan

@l.alex ,

PdfSaveOptions.IgnoreError is designed to handle exceptions during shape, image and chart rendering in pdf conversion.

We will prepare a file that validates setIgnoreError(true/false) working.

@l.alex ,

Preparing or finding a file that throws exception during pdf conversion is a bit difficult because we fixed almost every exception that we found.

When you converting the attched file “Book1.xlsx” to pdf, it works OK with JDK1.8. However, it will throw exception “Shape to image Error!” with JDK21. After setting setIgnoreError(true) , the exception is ignored and pdf conversion can be completed with JDK21.
Book1.zip (138.2 KB)

Indeed, I realise asking for a bug in a such well maintained library is hard to find, and even this rare option may be fixed in next version. However this is very helpful and sorts out our needs for now, thank you so much for you help!

@l.alex
Thank you for your feedback. You are welcome. If you have any questions, please feel free to contact us at any time.

Hi! Your help with the last two posts has been extremely valuable and continuing, I would appreciated if you could have a look at the following two as well:
Clarification on Overlap Between ExceptionType and WarningType in Aspose.Cells - Free Support Forum - aspose.com
Clarification on ExceptionTypes Where Recovery is Not Possible Using PdfSaveOptions.setIgnoreError(boolean) - Free Support Forum - aspose.com

@l.alex,

Thank you for your feedback. We value your evaluation of Aspose.Cells’ Exception module and encourage you to share your insights and concerns. Your input is always important to us.

Please see and follow up in your other threads for replies for your reference.