Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

try catch skipping exception

Tags:

c#

linq

wpf

try
{
    return strngarray.Select(strngarrayelem =>
    {
        string[] data = strngarrayelem .Split(',');

        return new xyzClass(data[1], data[2], data[0], (Color)System.Windows.Media.ColorConverter.ConvertFromString(data[3]), data.Length > 4 ? data[4] : "N/A");
    });
}
catch (Exception ex)
{
    MessageBox.Show("abc");
    return Enumerable.Empty<xyzClass>();
}

I am getting format exception in

(Color)System.Windows.Media.ColorConverter.ConvertFromString(data[3])

I try catching it by try-catch but exception is still thrown by app level try catch and not caught by my local try catch. Why my try catch not getting error ?

like image 562
murmansk Avatar asked May 07 '18 12:05

murmansk


People also ask

What happens if exception is not caught in try catch?

What happens if an exception is not caught? If an exception is not caught (with a catch block), the runtime system will abort the program (i.e. crash) and an exception message will print to the console.

Which is better throw or try catch?

Q #1) When to use throws throw VS try-catch in Java? Answer: The “throws” keyword is used to declare the exception with the method signature. The throw keyword is used to explicitly throw the exception. The try-catch block is used to handle the exceptions thrown by others.

What is a try catch exception?

Java try and catchThe try statement allows you to define a block of code to be tested for errors while it is being executed. The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.

Can we throw exception without try catch?

4. throws: The throws keyword is used for exception handling without try & catch block. It specifies the exceptions that a method can throw to the caller and does not handle itself.


1 Answers

You are just returning a LINQ query, it's not yet executed(like for example with ToList).

So if you want to catch the exception here you should consider materializing it to a collection in this method. You could still return IEnumerable<xyzClass> since List<xyzClass> implements that interface.

try
{
    return strngarray.Select(strngarrayelem =>
    {
        string[] data = strngarrayelem .Split(',');

        return new xyzClass(data[1], data[2], data[0], (Color)System.Windows.Media.ColorConverter.ConvertFromString(data[3]), data.Length > 4 ? data[4] : "N/A");
    }).ToList(); // <------- HERE !!!
}
catch (Exception ex)
{
    MessageBox.Show("abc");
    return Enumerable.Empty<xyzClass>();
}

If you don't know which method is just returning a query, look at the documentation in MSDN for the keyword deferred. For example Enumerable.Select:

This method is implemented by using deferred execution. The immediate return value is an object that stores all the information that is required to perform the action. The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach

Methods like for example Enumerable.ToList or ToArray call GetEnumerator, so they will execute the query. MSDN:

The ToList<TSource>(IEnumerable<TSource>) method forces immediate query evaluation and returns a List<T> that contains the query results. You can append this method to your query in order to obtain a cached copy of the query results.

ToArray<TSource> has similar behavior but returns an array instead of a List<T>.

like image 199
Tim Schmelter Avatar answered Sep 29 '22 01:09

Tim Schmelter