syntax error? maybe missing something - c#

Having a syntax error that I can not find I think. e.CmsData is showing error along with e.Message.
Error states: only assignment, call, decrement, and the new object expressions can be used as a statement.
What am I missing?
private static void OnMessageReceived (object sender, MessageReceivedEventArgs e)
{
try
{
if (e == null)
return;
if (e.CmsData != null) e.CmsData;
if (!String.IsNullOrEmpty(e.Message))
(e.Message);
}
catch (Exception ex)
{ }
{
// logger.Error(" Exception " + ex);
// throw ex;
}
}

e.CmsData;
is not a valid statement, you need to do something with it, like
var x = e.CmsData;
The same goes for
(e.Message);

Accessing a property like you did is invalid
e.Cmsdata; // Invalid
Properties are just like variables but encapsulated.

You're referencing variables but not doing anything with them.
Try something like this
private static void OnMessageReceived (object sender, MessageReceivedEventArgs e)
{
try
{
if (e == null)
return;
(e.CmsData != null)
{
var data = e.CmsData;
//Do something with "data"
}
if (!String.IsNullOrEmpty(e.Message))
MessageBox.Show(e.Message);
}
catch (Exception ex)
{
// logger.Error(" Exception " + ex);
// throw ex;
}
}

Related

when to use await key word? do I need it in this case?

i have a onClick method on a buttom. when this button is pressed, i get a displayalert message.
getTokenQ has a value. my question why its going inside catch block?
private async void Button_Clicked(object sender, EventArgs e)
{
try
{
var getTokenQ = await SecureStorage.GetAsync("Save_Security_Question");
if ((String.IsNullOrWhiteSpace(getTokenQ) == false))
{
}
else
{
}
}
catch (Exception ex)
{
await DisplayAlert("Message", "Some thing went wrong. Please try different method", "ok");
}
}
no you don't need to do that, you can use .Result if you don't want to execute your code asynchronously , this code is perfectly fine :
private void Button_Clicked(object sender, EventArgs e)
{
try
{
var getTokenQ = SecureStorage.GetAsync("Save_Security_Question").Result;
if ((String.IsNullOrWhiteSpace(getTokenQ) == false))
{
}
else
{
}
}
catch (Exception ex)
{
DisplayAlert("Message", "Some thing went wrong. Please try different method", "ok");
}
}
Why in catch ! Because it's a message to display only if an exception is thrown.

How to catch all exceptions in try catch block in Xamarin.Android

How to catch all exceptions in try catch block in Xamarin.Android
I am very frustrated on how Xamarin.Android handles unhandled exception which is very weird, I added three exceptions for all api queries respectively:
try
{
// api query using `refit`
// json parsing using `newtonsoft`
}
catch(System.OperationCanceledException e)
{
// user cancelled the query, show option to retry
}
catch(ApiException apiException)
{
// theres an api exception , show error message to users , show option to retry
}
catch(Exception e)
{
// unknown exception ignore , show error message to users , show option to retry
}
This try catch blocks works most of the time, but there is one certain scenario when our server is down, and it just throws exception and crashes the app over and over again until the server is back up.
This is the exception that keeps on bugging us :
Xamarin caused by: android.runtime.JavaProxyThrowable: Newtonsoft.Json.JsonReaderException
As you can see in JsonReaderException hierarchy, it inherited System.Exception which is the last catch block i used.
and I checked this JsonReaderException it extends from Exception , In which our try catch block should handle it.
Now im wondering is there any way that we can catch all those pesky unhandled exceptions?
I'm getting unhandled exceptions in this way
public void Init()
{
AndroidEnvironment.UnhandledExceptionRaiser += OnAndroidEnvironmentUnhandledExceptionRaiser;
AppDomain.CurrentDomain.UnhandledException += OnCurrentDomainUnhandledException;
TaskScheduler.UnobservedTaskException += OnTaskSchedulerUnobservedTaskException;
var currentHandler = Java.Lang.Thread.DefaultUncaughtExceptionHandler;
var exceptionHandler = currentHandler as UncaughtExceptionHandler;
if (exceptionHandler != null)
{
exceptionHandler.SetHandler(HandleUncaughtException);
}
else
{
Java.Lang.Thread.DefaultUncaughtExceptionHandler = new UncaughtExceptionHandler(currentHandler, HandleUncaughtException);
}
}
private void OnAndroidEnvironmentUnhandledExceptionRaiser(object sender, RaiseThrowableEventArgs e)
{
AndroidEnvironment.UnhandledExceptionRaiser -= OnAndroidEnvironmentUnhandledExceptionRaiser;
_logger.LogFatal($"AndroidEnvironment.UnhandledExceptionRaiser.", e.Exception);
e.Handled = true;
}
private void OnCurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
AppDomain.CurrentDomain.UnhandledException -= OnCurrentDomainUnhandledException;
var ex = e.ExceptionObject as Exception;
if (ex != null)
{
_logger.LogFatal("AppDomain.CurrentDomain.UnhandledException.", ex);
}
else
{
_logger.LogFatal($"AppDomain.CurrentDomain.UnhandledException. ---> {e.ExceptionObject}");
}
}
private void OnTaskSchedulerUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
{
_logger.LogFatal("TaskScheduler.UnobservedTaskException.", e.Exception);
}
private bool HandleUncaughtException(Java.Lang.Throwable ex)
{
_logger.LogFatal("Thread.DefaultUncaughtExceptionHandler.", ex);
return true;
}

Catching errors with .NET Task Parallel Library

Here are the two alternatives i tried for catching errors, they both seem to do the same thing.. but is one preferable over the other and why ?
Alt 1:
private async void BtnClickEvent(object sender, RoutedEventArgs e)
{
try
{
Task t = Task.Run(() =>
{
_someObj.SomeMethod();
});
await t; //wait here, without blocking...
}
catch (Exception ex)
{
string errMsg = ex.Message + Environment.NewLine;
errMsg += "some unhandled error occurred in SomeMethod";
Log(errMsg);
return; //<-- bypass below code on error...
}
//other code below... does not execute...
DoSomethingElse();
}
Alt 2:
private async void BtnClickEvent(object sender, RoutedEventArgs e)
{
bool errOccurred = false;
Task t = Task.Run(() =>
{
try
{
_someObj.SomeMethod();
}
catch (Exception ex)
{
string errMsg = ex.Message + Environment.NewLine;
errMsg += "some unhandled error occurred in SomeMethod";
Log(errMsg);
errOccurred = true;
}//end-Catch
});
await t; //wait here, without blocking...
if (errOccurred) return; //<-- bypass below code on error...
//other code below... does not execute...
DoSomethingElse();
}
Better option is to refactor part of the code into a separate method returning a bool indicating that whether to proceed or not.
private async void BtnClickEvent(object sender, RoutedEventArgs e)
{
bool success = await SomeMethodAsync();
if (!success)
{
return;
}
//other code below... does not execute...
DoSomethingElse();
}
private async Task<bool> SomeMethodAsync()
{
try
{
await Task.Run(() => _someObj.SomeMethod());
return true;
}
catch (Exception ex)
{
string errMsg = string.Format("{0} {1}some unhandled error occurred in SomeMethod",
ex.Message, Environment.NewLine);
Log(errMsg);
return false;
}
}
It's better to refactor the code than putting it all at the same place.It's better to catch the exception within your delegate if all you need to do is log it.
private async void BtnClickEvent(object sender, RoutedEventArgs e)
{
await Task.Run(() =>
{
try
{
DoSomeWork();
}
catch (Exception ex)
{
log.Error(ex.Message);
}
});
}
However If you have another method DoSomethingElse() which might be affected by the outcome of the Task.It's better to wrap try catch around await
private async void BtnClickEvent(object sender, RoutedEventArgs e)
{
try
{
await Task.Run(() =>
{
try
{
DoSomeWork();
}
catch (Exception ex)
{
log.Error(ex.Message);
}
});
DoSomethingElse();
}
catch(Exception ex)
{
}
}
As with anything it depends.
I'd say refactor the Task.Run() section into a separate async Task method, much like Sriram Sakthivel's answer, is in general a good thing. It avoids the use of a captured bool in the lambda as in version 2, and it lets you write code that expresses intent more concisely.
That said, I would carefully consider if the "catch all -> log -> ignore" pattern is what you want. In general: catch specific exceptions and handle them specifically. For all other exceptions, you might log them, but still rethrow them with "throw;" or "throw new MoreSpecificException(originalException);".
With that in mind I would suggest that if you do the catch all approach you should do the catch all as in version 1.
To keep readability high, make the code concise with clear intent, and be explicit about handling exceptions, I would write it something like this:
private async void BtnClick(object sender, RoutedEventArgs e)
{
try
{
if (await TryDoSomethingAsync())
{
DoSomeMoreStuff();
}
}
catch (Exception ex)
{
// I am sure it is fine that any and all exceptions can be logged and ignored.
Log(ex);
// And maybe even notify the user, since I mean, who monitors log files anyway?
// If something that shouldn't go wrong goes wrong, it's nice to know about it.
BlowUpInYourFace(ex);
}
}
private async Task<bool> TryDoSomethingAsync()
{
return await Task.Run<bool>(() =>
{
try
{
_myService.DoSomething();
}
catch (SomeKnownException ske)
{
// An expected exception which is fine to ignore and return unsuccessful.
Log(ske);
return false;
}
catch (SomeOtherKnownException soke)
{
// Expected exception that indicates something less trivial, but could be more precise.
throw new MyMorePreciseException(soke);
}
// Nothing went wrong, so ok.
return true;
});
}

The inner-most exception in c#

Is there a way to get the inner-most exception without using :
while (e.InnerException != null) e = e.InnerException;
I'm looking for something like e.MostInnerException.
To extend on Hans Kesting's comment, an extension method might come in handy:
public static Exception GetInnerMostException(this Exception e)
{
if (e == null)
return null;
while (e.InnerException != null)
e = e.InnerException;
return e;
}
Here is another answer that differs a bit: you can create an enumerator.
With
public static IEnumerable<Exception> EnumerateInnerExceptions(this Exception ex)
{
while (ex.InnerException != null)
{
yield return ex.InnerException;
ex = ex.InnerException;
}
}
You can do
try
{
throw new Exception("1", new Exception("2", new Exception("3", new Exception("4"))));
}
catch (Exception ex)
{
foreach (var ie in ex.EnumerateInnerExceptions())
{
Console.WriteLine(ie.Message);
}
}
So, technically, you are not longer using a while loop a visible way :)

Error message handling due to exceptions

i have a code to restart a service in an event which does other functions too.
I have a try catch in the event for everything within the event like this:
private void btnApply_Click(object sender, EventArgs e)
{
try
{
applyChangesAndCheckRestartService();
}
catch (Exception ex)
{
MessageBox.Show("Error loading page.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void applyChangesAndCheckRestartService()
{
string svrPortNo = CommonCodeClass.getTagValue(CommonCodeClass.xml_SvrPortNoTag, CommonCodeClass.configLocation + CommonCodeClass.configXML);
if (!ApplyChangesForSettings())
{
return;
}
if (svrPortNo != tbSvrPortNo.Text)
{
CommonCodeClass.CheckToRestartService();
}
}
Now if there is an error during ApplyChangesForSettings() i will get an error popup "Error loading page".
If there is an error in CheckToRestartService() i will get the same error because of the try catch.
Is there a better way to handle this.
Like i dont mind the error loading page for ApplyChangesForSettings() but for CheckToRestartService() i would like to see an error like "unable to restart service".
Any suggestions are appreciated.
Thanks
internal static void CheckToRestartService()
{
DialogResult result = MessageBox.Show(CommonCodeClass.resartServiceMessage, "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (result == DialogResult.Yes)
{
CommonCodeClass.RestartService(CommonCodeClass.serviceName, 60000);
}
}
Do they throw different exceptions? If they do you could use exception filtering:
private void btnApply_Click(object sender, EventArgs e)
{
try
{
applyChangesAndCheckRestartService();
}
// catch service start exceptions
catch (InvalidOperationException ioex)
{
// display message that couldn't start service
}
// catch rest
catch (Exception ex)
{
MessageBox.Show("Error loading page.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
UPDATE this is assuming you're calling something like ServiceController.Start() which throws InvalidOperationException on failure, you could easily throw this yourself on your own error condition or create your own custom exception.
if (/* service didn't start */)
{
throw new InvalidOperationException("Could not start service.");
}
You either need to
catch the exception in applyChangesAndCheckRestartService
or you could pass an enum by ref f.e. called RestartStatus
enum RestartStatus{success, unableToRestart, unableToApplySettings};
RestartStatus status = RestartStatus.success;
applyChangesAndCheckRestartService(status);
if(status != RestartStatus.success) //....
private void applyChangesAndCheckRestartService(out RestartStatus status)
{
// set the status variable accordingly
}
A third way is to use custom exceptions that you can catch separately.
Well maybe you just need to wrap the different functions with separate try/catch blocks:
try {
if (!ApplyChangesForSettings())
return;
}
catch (Exception ex) {
MessageBox.Show("Error loading page.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
if (svrPortNo != tbSvrPortNo.Text) {
try {
CommonCodeClass.CheckToRestartService();
}
catch (Exception ex) {
MessageBox.Show("Unable to restart services.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Or, you could consider catching different types of exceptions, if they threw different types:
string errmsg = string.empty;
try {
DoSomething();
}
catch (FooException) {
errmsg = "There was a Foo error";
}
catch (WidgetException) {
errmsg = "There was a problem with a Widget";
}
catch (Exception ex) {
errmsg = "General problem: " + ex.Message;
}
if (!string.IsNullOrEmpty(errmsg))
MessageBox.Show(errmsg);
See also:
Exception Handling
The fastest way to handle this situation is throwing an exception when someone of your internal methods fails and catch the message in the btnApply_Click.
MessageBox.Show(ex.Message, "Error", .....);
The rightest way is to create your own exception type and inside the methods, if there is a fail condition throw your own exception. For example create a class like this
public class RestartServiceException : Exception
{
public RestartServiceException(string message)
: base(message)
{
}
// You could also write other constructors with different parameters and use internal logic
// to process your error message
}
and then, use an instance of that class when the fail condition arise inside your CheckToRestartService method
if(fail == true)
throw new RestartServiceException("The service could not be started because .....");

Categories

Resources