2

In C++ if I want to exit a method without returning anything I can do;

// A completely useless method static public double function(int a){ if(a==1){ cout << "Error !"; exit(1); } else return (double)a; } 

How can I do the equivalent in C# ?

1
  • Don't use the exit() function. It should be called "crash()", because that's what it actually does. Do you really want to be crashing your program in the middle of a function? Commented Mar 31, 2009 at 0:47

9 Answers 9

14

The usual way of handling an error condition in .NET is to throw an exception:

public static double function(int a) { if (a == 1) { throw new ArgumentException("The value 1 is not accepted."); } return (double)a; } 

The exception would be caught by the code calling the method, or somewhere down the line. It's up to the calling code to handle it at an appropriate level.

It's quite usual for methods to sanitise the input in this manner, so that any faulty values are caught early instead of causing an error later in the code where it is much harder to track down.

Sign up to request clarification or add additional context in comments.

2 Comments

I'd argue though, that you shouldn't be catching ArgumentException. That's strictly an exception for the caller.
You should handle any exception (and only those exceptions) that you can intelligently handle and recover from.
12

The "exit" function doesn't just exit the method - it terminates the program.

http://msdn.microsoft.com/en-us/library/k9dcesdd.aspx

In C# Console Applications, you could call System.Environment.Exit().

But I would suggest that any code doing this could be written with much better structure. For example, your method could throw an exception, and let the caller decide how to handle it.

Comments

7

You can exit a method without returning anything by using the return statement. But if the return type is anything apart from void you will have to return something.

If you want to get out of a method because of an error, you should look at exceptions.

Comments

3

http://msdn.microsoft.com/en-us/library/system.environment.exit.aspx

Comments

0

That would certainly be an odd thing to do, but yeah - you could call Environment.Exit. I'd strongly suggest throwing an Exception instead though. That gets you a chance to cleanup, and your caller can decide how to handle the error. If nobody does, you'll still bring down the process - but somewhat more gracefully.

Comments

0

Considering the context of what you want to do, this should be the equivalent:

public static double SomeFunction(int a) { if(a==1) { throw new Exception("Error!"); } else { return (double)a; } } 

1 Comment

Generally speaking yes, but note to Shinka, you'd likely want to be a lot more precise about the Exception type. i.e. NotSupportedException, ArgumentException, ArgumentOutOfRangeException.
0

try return to exit out of the method without exiting the whole program.

1 Comment

That only applies to void methods. What about other types? You need to return some value.
0

Another option taking into account all the previous answers, and in the case you are working with Windows Forms is to call

Application.Exit(); 

Comments

0

You don't. By following your method, you'd have to do a test after every call to your function, to check if a double was in fact returned.

In your example, you'd do something like...

public static double myFunc( int a ) { if( a==1 ){ throw new InvalidArgumentException( "a must be greater than one." ); } return (double)a; } 

Comments