how to exit of function which has return a type but i want to exit the function from in between i.e. not executing rest of function.

public LaunchData CalculateLaunchData() {

if(playerControll == true) {
balllauncher.PlayerHandle ();
return null; // execution error what i do it for solving the issue
return new LaunchData(vector3, float);

If your function returns a void, just type return; or return null;. You can also throw a exception.

try just this


A method can be exit in two ways:

  • return
  • throw an exception.

In almost all cases you want to use “return” to exit a method. This is called a controlled exit. Of course inside a method with return type you have to return something. If your return type is a reference type you can simply return “null”. Keep in mind that the calling method need to check the value that you returned before using it.

Another way to exit a method is to throw an exception. However exceptions should only be used as last resort. Exceptions terminate the method forcefully as the result of an unrecoverable error. Exceptions should not be used as a control statement.

Keep in mind that a thrown exception that is not catched in the calling method will terminate all methods up the stacktrace until the exception is caught or it reaches the top. In a “normal” application that would result in an application crash and the application gets terminated by the OS. Unity however will catch exceptions at the point where they invoke a callback. So an exception thrown inside Update will terminate this Update call and continue. Though watch out: Exceptions have a lot of processing and memory / garbage overhead.

When an exception is thrown inside a method you don’t need / can return a value as the method terminated abnormally.

An exceptions is actually just a class instance that is derived from System.Exception. You can throw a generic exceptions like this;

throw new System.Exception("Some error message");

However it’s generally recommended to create your own exception class.