tags:

views:

118

answers:

6

i have the following code which show error from a string when there is a error

is there any way that i can make this in catch block?

  main()
    {
    try
    {
    if (a==1)
    {
     string = "error1"
    }
    elseif (a==2)
    {
     string = "error2"
    }
    }
    }
    catch (e)
    {


    }

Like below:

i have the following code which show error from a string when there is a error

is there any way that i can make this in catch block?

main()
{
try
{
}
}

catch (e)
{

if (a==1)
{
 string = "error1"
}
elseif (a==2)
{
 string = "error2"
}

}
+3  A: 

You're making an absolute hash of the syntax I'm afraid. I suggest that before anything else you go read the MSDN article on try-catch statements. The syntax and semantics of exception handling should then start to make sense.

Noldorin
A: 

Why would you want to put logic into a catch block?

You should read up on exception handling to see what the correct syntax is, and why you would use it in the first place.

http://www.csharp-station.com/Tutorials/lesson15.aspx

A good book on c# might also be useful, you could try Essential C# as it is useful for beginners...

Longball27
A: 

This is not the right way to use exception handling. Exceptions should be exceptional, and not used for conditional logic.

All you need is your if ... else block.

Russ C
+1  A: 

Even though it is an improper version, it may well answer your question:

    try {

        if (a == 1) {
            throw new Exception("error1");
        } else if (a == 2) {
            throw new Exception("error2");
        }

    } catch (Exception ex) {
        Console.WriteLine("Errormessage = " + ex.Message);
    }
weberph
A: 

You probably want to do something like this.

 try {

 if(a == 1) {throw new Exception("Erorr1");}

 if(a == 2) {throw new Exception("Erorr2");}

 } catch(Exception e) {
   /*Do something with exception or look below */
 }

Here you raise an error by throwing an Exception that is served in catch clause

or this

try{
 /*Some code that cause an Exception*/
} catch(Exception e) { //Exception describe the type which wold be caught. 

 string errorType = String.Empty;

 var a = -1; //Assign to it smtg from e that You want to check

 if(a == 1) {
   errorType = "erro1";
 }
 else  if (a == 2) {
   errorType = "erro2";
 }

 /* More Your code*/

}

hire You do something with error type that You caught.

But first of all read about something that You want to use in Your code.

Vash
This is probably the worse practise for using exceptions.Making a decision based on the value of a variable is not the reason one should throw an exception, its the reason one should use an 'if' statement.
Russ C
It is a debatable practice, for instance when we have the null is some field/parameter system throws an error when we try to work with it but if we display that null on the console nothing wrong happen. Also a calculation can be stop this way in some complex algorithms. Nevertheless we can only assume what SmartestVEGA wanted.
Vash
A: 

Read up on Baseball Exception Handling and why not to do it.

Jesse C. Slicer