rysahara
rysahara

Reputation: 356

pass value to catch errors occurring in the try

have a function in inside the try, and would like that when an error occurs during the process, a specific value is passed to the catch. How can I do this?

try
{
   value=x;
   function(....);

}
catch(Exception ex)
{
   messageError(....)
   function(x);
}

Upvotes: 1

Views: 10867

Answers (2)

Ajay2707
Ajay2707

Reputation: 5798

Its related to scope, you just add a variable out of try catch block (as above suggest).

public/protected string methodname()
{
  string strVariable = "";

  try
  {
    strVariable = "No Error";
  }
  catch(Exception EX)
  {
    strVariable = "Error";
  }

  return strVariable ;
}

Even if you want to access a variable globally in a page. You must declare at the intiation of class.

public class classname 
{
   public string strVariable = "";
}

Upvotes: 1

Guillaume
Guillaume

Reputation: 13138

You must declare a variable for the value before the try block, or pass this value Inside the exception (throw a custom exception containing the value you need for this error).

string value = null;
try
{
   value=x;
   function(....);

}
catch(Exception ex)
{
   messageError(....)
   if (value != null)
       function(value);
}

Upvotes: 9

Related Questions