How to rebuild exception and return value in C #?

Given the following code, how can I return some value (DateTime.Now.ToString ()) if an exception is thrown?

public string DateToString(int year, int month, int day) { try { DateTime datetime = new DateTime(year, month, day); return datetime.ToString(); } catch (Exception) { //log error and rethrow throw; } } 
+4
source share
5 answers

When you throw an exception, your method ends immediately.
It is also impossible to return a value.

When you call a method that throws an exception, control immediately passes to the catch .
You will not be able to observe or use the return value (nonexistent).

You need to rethink your design.

+11
source

I think your question is incorrectly worded. It looks like you just want to get the default value for the exception. therefore

 public string DateToString(int year, int month, int day) { try { DateTime datetime = new DateTime(year, month, day); return datetime.ToString(); } catch (Exception exObj) { //log error LogMyError(exObj); return DateTime.Now.ToString(); } } 
+3
source

I recommend you do this as follows:

  bool IsValidDateTime(int year, int month, int day, out DateTime result) { try { result = new DateTime(year, month, day); return true; } catch (System.Exception ex) { result = DateTime.Now; // assign a value return false; } } 

If you want the string to just make sure the return value is true and call

  result.ToString(); 
+2
source

Think about what the exception actually entails. If your function returns a value, maybe you want to use it somewhere, right?

how

 // Will a value be assigned to dateString, or will an exception be thrown? string dateString = DateToString(2011, 2, 29); 

If you want DateToString return a value in the exceptional case above, then you want this value to be assigned to dateString , right? But what do you want to do with the exception? Either you are going to continue, or not; you cannot have it in both directions.

+1
source

Edit: cyberkiwi understood the question better than me, but I will let him seem like a bad reading example;)


You can always define your own exception class:

 [Serializable()] public class OwnException : System.Exception { public readonly MaybeDateTime; ... public OwnException (string message, System.Exception inner) : base(message, inner) { maybe = null; } public OwnException (string message, System.Exception inner, DateTime maybe) : base(message, inner) { MaybeDateTime = maybe; } } 

and drop it (date and time assignment).

You will need to transfer the datetime DateTime declaration outside of the try-catch block and in your example SLaks is right: it does not make sense. datetime can only contain something meaningful if return-Statement throws a catch exception. In other cases, there may be useful applications.

Edit: The same thing can be done with the string, but in order to restore this as an exception, if returning the current time solves the situation in a good way, it will be somewhat idiotic. Did not read correctly as stated above.

+1
source

Source: https://habr.com/ru/post/1337716/


All Articles