cancel
Showing results for 
Search instead for 
Did you mean: 

How to throw exception

AbdulHammed
Explorer
0 Kudos

Hi Experts,

My Scenario is File(Tab delimiter) to proxy and the requirement is when ever i get an invalid char any where in file have to throw an exception.

Can you please suggest me guys.

Regards

Abdul.

Accepted Solutions (0)

Answers (2)

Answers (2)

former_member182412
Active Contributor
0 Kudos

Hi Abdul,

Use below java mapping before message mapping to throw an exception if there are any special characters.


import java.io.InputStream;

import java.io.OutputStream;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

import com.sap.aii.mapping.api.AbstractTransformation;

import com.sap.aii.mapping.api.StreamTransformationException;

import com.sap.aii.mapping.api.TransformationInput;

import com.sap.aii.mapping.api.TransformationOutput;

public class CheckSpecialCharacters extends AbstractTransformation {

  @Override

  public void transform(TransformationInput transformationInput, TransformationOutput transformationOutput)

  throws StreamTransformationException {

  try {

  InputStream inputStream = transformationInput.getInputPayload().getInputStream();

  OutputStream outputStream = transformationOutput.getOutputPayload().getOutputStream();

  byte[] buf = new byte[inputStream.available()];

  inputStream.read(buf);

  String inputPayload = new String(buf);

  Pattern pattern = Pattern.compile("[^\\p{ASCII}]");

  Matcher matcher = pattern.matcher(inputPayload);

  if (matcher.find())

  throw new StreamTransformationException("File got invalid characters");

  outputStream.write(inputPayload.getBytes());

  } catch (Exception ex) {

  getTrace().addDebugMessage(ex.getMessage());

  throw new StreamTransformationException(ex.toString());

  }

  }

}

Test mapping:

Regards,

Praveen.

former_member186851
Active Contributor
0 Kudos

Hello Abdul,

If the Invalid characters are causing the mapping failure you can trigger an alert and mail will be sent to configured users.

Refer the below links

AbdulHammed
Explorer
0 Kudos

Thanks Raghu,

But mapping is not failing when i tried to test with invalid char file.

can u please suggest me an UDF to throw an exception at mapping level.

iaki_vila
Active Contributor
0 Kudos

Hi Abdul,

Check this blog Throwing Smart Exceptions in XI Graphical Mapping | SCN or Debashish answer UDF to raise exception | SCN

Regards.

former_member186851
Active Contributor
0 Kudos

Abdul,

Links shared by Inaki should help you.

Once more link for your reference

SAPTechnical.COM - Handling and Tracing Runtime Exceptions in Java Mapping (SAP PI/XI)