0

I have a code like this,

using (SPSite site = new SPSite("http://site/"))
{    
    using (SPWeb web = site.OpenWeb())
    {
        try
        {
            SPList list = web.Lists["ListName"]; // 2        
            SPListItem item = list.Items.Add();
            Guid itemId = item.UniqueId;
            SPListItem itemUpdate = web.Lists["ListName"].Items[itemId];
            itemUpdate["PercentComplete"] = .45; // 45%    HERE IS EXCEPTION      
            itemUpdate.Update();
        }
        catch (Exception e)
        { 
            Console.WriteLine(e);
            Console.ReadLine();

        }
    }
}

I am getting this exception on line itemUpdate["PercentComplete"]

Value does not fall within the expected range.

What I want is

I want this exception to be ignored as if it returns null then keep it null instead of throwing exception,

I already tried this,

Object letSay = itemUpdate["PercentComplete"]; 
// thought object can be null but same exception

I don't want to try

try {} and Catch {} either.
10
  • 1
    Debug your code and see where it is failing. Then line before Check if the value is null or not. Commented Aug 28, 2013 at 12:30
  • The key to this is the implementation of SPListItem. That's where the exception is originating. Commented Aug 28, 2013 at 12:31
  • @Dilshod I already mentioned where it is failing and the exception it's giving Commented Aug 28, 2013 at 12:31
  • @user13814 I saw that, just didn't wanted to copy that code to comment. Commented Aug 28, 2013 at 12:32
  • Note the question title suggests NullReferenceException, but the message "Value does not fall within the expected range." suggests otherwise. Which is it? Commented Aug 28, 2013 at 12:33

3 Answers 3

1

You just need to check this field existance:

SPListItem item = list.Items.Add();
if (item.Fields.ContainsField("PercentComplete"))
{
    item["PercentComplete"] = .45;
}
item.Update();
Sign up to request clarification or add additional context in comments.

Comments

1

Not sure because I don't use Sharepoint,but looking at the docs you need to create the field "PercentComplete" before trying to set a value in it.

SPListItem itemUpdate = web.Lists["ListName"].Items[itemId];
itemUpdate.Fields.CreateNewField("PercentComplete", "PercentComplete");
itemUpdate["PercentComplete"] = .45; 

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splistitem.fields.aspx
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfieldcollection_methods.aspx

Let's hope an expert on SharePoint could give you a better answer.....

As a side note: There is no way to ignore an exception. Exceptions are an 'exceptional' event. Something that you cannot expect, not something that you could prevent to happen with proper coding. Accessing an item that doesn't exist is a bad practice and you could easily avoid it. If you wish you could setup a global exception handler that handles all the uncaught exception adding code like this to your main method

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
AppDomain.CurrentDomain.UnhandledException += 
         new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

And then prepare the following methods

private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
    string msg = "Not recognized error:" + e.Exception.Message;
    if (e.Exception.InnerException != null)
    {
           msg = msg + "\r\nPrevious error:" + e.Exception.InnerException.Message;
    }
    msg = msg + "\r\nStackTrace:" + e.Exception.StackTrace;
    msg = msg + "\r\nDo you wish to continue with the application?";
    DialogResult dr = AskAQuestion(msg);
    .. add logging here ...
    if (dr == DialogResult.No) Application.Exit();
}    

private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    Exception ex = e.ExceptionObject as Exception;
    if (ex != null)
    {
        string msg = "Not recognized error:" + e.Exception.Message;
        if (e.Exception.InnerException != null)
        {
               msg = msg + "\r\nPrevious error:" + e.Exception.InnerException.Message;
        }
        msg = msg + "\r\nStackTrace:" + e.Exception.StackTrace;
        .. add logging here ...
    }
}

2 Comments

thanks but i wanted to know how can i ignore exception in c#, above code is just an example sir
@user13814 You can ignore an exception by using a try-catch block. There is no other way of doing so, if you cannot change the class throwing the exception.
0

According to your comment to Steve's answer:

but i wanted to know how can i ignore exception in c#, above code is just an example sir

You cannot ignore an exception in C# without using a try-catch-block. In your case the code should look like this:

try
{
    itemUpdate["PercentComplete"] = .45; // 45%    HERE IS EXCEPTION
}
catch
{
}

But this code is neither nice, nor should one simply ignore exceptions in first place!

2 Comments

Please read my question, i already mentioned I don't want to use this, thanks
@user13814 please read my answer. There is no other way in C# to ignore execeptions. You can of cause make sure that exceptions don't happen in first place - but that's not ignoring them.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.