AX 2012 R3 Visual Studio project C# parsing errors - c#

I am writing a wrapper for SSHnet dll from renci (https://sshnet.codeplex.com/), so I can fetch files from a sFTP server via Dynamics AX periodically.
When I catch errors in C# project for example in a try catch statement how can I parse the error message back to AX?
Options I thought of;
Should I put the error message in a string variable and read the string in Dynamics AX?
Write errors to the event log on the AOS / client?

The easiest way to pass error to AX is a rethrow it in C#
catch (Exception ex)
{
// Do special cleanup, logging etc.
throw;
}
and catch it in AX
catch (Exception::CLRError)
{
ex = ClrInterop::getLastException();
if (ex != null)
{
ex = ex.get_InnerException();
if (ex != null)
{
error(ex.ToString());
}
}
}

Related

How to catch Ignite thin .net client exception

I am using Apache Ignite 2.7.5.
I don't know whether is there any way is there or not but trying to handle .net thin client exception,but i am not approaching to correct way,how to catch ignite java one throwable exception in c# .net catch block.
code:
public async void loadData(string configPath,List<JObject> dataList)
{
using (var ldr = _ignite.GetDataStreamer<string, Employee>(cacheName))
{
try
{
await ldr.AddData(base64EncodedKey, emp);//from this line i am getting exception on console but not able to catch
}
catch (Exception ex)//how to catch here java one throw exception
{
}
}
}
Any one suggest me correct approach to get exception in catch block.

C# fails to catch error in opening a database connection?

I have written a function in my windows forms application that will return true or false based on the success in opening a connection to a database however my try catch block fails to catch the exception that is thrown.
private bool TestConnect()
{
try
{
conn.Open();
return true;
}
catch (OleDbException e) { return false; }
}
Error thrown:
System.Data.OleDb.OleDbException
Please help, I can not for the life of me figure this one out!
I figured it out, I needed to alert VS that I wanted to Except this error

Sync conflict handling in Azure Mobile Services: ad hoc vs sync handler

When not using a "sync handler", one needs to catch sync errors for push and pull.
PUSH:
Samples say to catch MobileServiceInvalidOperationException, MobileServicePushFailedException and Exception:
try {
await client.SyncContext.PushAsync();
}
catch (MobileServiceInvalidOperationException ex) {
// ...push failed
// ...do manual conflict resolution
}
catch (MobileServicePushFailedException ex) {
// ...push failed
// ...do manual conflict resolution
}
catch (Exception ex) {
// ...some other failure
}
PULL:
Samples say to catch MobileServiceInvalidOperationException and Exception:
try {
await syncTable.PullAsync("allitems", syncTable.CreateQuery());
}
catch (MobileServiceInvalidOperationException ex) {
// ...pull failed
}
catch (Exception ex) {
// ...some other failure
}
SYNC HANDLER:
Errors are processed in .ExecuteTableOperationAsync(). Samples say to catch
MobileServiceConflictException, MobileServicePreconditionFailedException and Exception.
FINALLY A QUESTION(S):
I hope I covered all the possible exceptions types above.
If I use a sync handler, does that mean I don't need to try-catch the push/pull/purge/etc. operations? Samples I've seen are a little confusing as they include everything (ad hoc resolution and sync handler) in the same project...
You should always place push/pull/etc. operations in a try/catch block. There is always a risk that an Exception you haven't thought of (including the network going away, for example) will happen.

Catching IOException

I am writing a C# application in which I have to display a message if File is already being used by some process and if the file doesnot exist, the application needs to display another message.
Something like this:
try
{
//Code to open a file
}
catch (Exception e)
{
if (e IS IOException)
{
//if File is being used by another process
MessageBox.Show("Another user is already using this file.");
//if File doesnot exist
MessageBox.Show("Documents older than 90 days are not allowed.");
}
}
Since IOException covers both the conditions, how do I distinguish if this exception is caught because of File being used by another process or File doesnot exist?
Any help would be highly appreciated.
Always catch from the most specific to the most generic exception type.
Every exception inherits the Exception-class, thus you will catch any exception in your catch (Exception) statement.
This will filter IOExceptions and every else separately:
catch (IOException ioEx)
{
HandleIOException(ioEx);
}
catch (Exception ex)
{
HandleGenericException(ex);
}
So catch Exception always last. Checking with if is possible, but not common.
About your problem:
if (File.Exists(filePath)) // File still exists, so obviously blocked by another process
This would be the simplest solution to separate your conditions.
As you can see here File.OpenRead can throw these exception type
ArgumentException
ArgumentNullException
PathTooLongException
DirectoryNotFoundException
UnauthorizedAccessException
FileNotFoundException
NotSupportedException
for each of this exception type you can handle it in this way
try{
}
catch(ArgumentException e){
MessageBox.Show("ArgumentException ");
}
catch(ArgumentNullExceptione e){
MessageBox.Show("ArgumentNullExceptione");
}
.
.
.
.
catch(Exceptione e){
MessageBox.Show("Generic");
}
In your case you can handle just one or two types and other are always catched by generic Exception (it must be always the lastone because cathces all Exceptions)
Try the following:
try
{
//open file
}
catch (FileNotFoundException)
{
MessageBox.Show("Documents older than 90 days are not allowed.");
}
catch (IOException)
{
MessageBox.Show("Another user is already using this file.");
}
More info: http://www.dotnetperls.com/ioexception
I know this is old, but the only consistent solution to do this is to filter catch blocks by the HResult property. I don't know which one it is but here's an example for copying a file and catching if the file already exists :
try
{
File.Copy(source, dest, false); // Try to copy the file normally
}
catch (IOException e) when (e.HResult == -2147024816) // 0x80070050 : The file already exists
{
// Prompt user for overwrite...
}
Consult the .NET reference source and track the Win32 calls to find the returned HResult.
When file not exists it will throw the FileNotFoundException that inherit IOException, So you can write like this:
try
{
//file operate
}
catch (FileNotFoundException ex)
{
MessageBox.Show("Documents older than 90 days are not allowed.");
}
catch (IOException ex)
{
MessageBox.Show("Another user is already using this file.");
}

Getting Error: "underlying connection closed: unexpected error on request receive"

I am using .Net application with web service(.net) and oracle database. But when i send some request to the server and waiting for the result it is giving following error
The underlying connection was closed: An unexpected error occurred
I am unable to get the cause of the error. Can any one plese help me to get actual cause of this error and what i need to do solution. This errror occurce only in Production Server. Same code in Dev and Test server it is working.
This is probably caused by the error inside web service method or function that you are calling. Just create proper error handling and log web service exception. Here is my sample code:
[WebMethod]
public void MyServerMethod()
{
try
{
//open connection and execute your calls to Oracle DB...
}
catch (Exception ex)
{
LogServiceException(ex);
throw ex;
}
}
void LogServiceException(Exception ex)
{
string fullMessage = ex.Message;
while (ex.InnerException != null)
{
ex = ex.InnerException;
fullMessage += " Inner exception: " + ex.Message;
}
//log your exception to log file, DB or eventlog...
//in this case I will use log file, just make sure you appropriate filesystem rights to do this...
System.IO.File.AppendAllText("LogFile.txt", fullMessage);
}

Categories