How to indiciate a failure for a function with a void result

how to call a void function in c
void function c++
void return type in c
void function example
return in void function c
void function in c example
return void function c++
void function python

I have a function in scala which has no return-value (so unit). This function can sometimes fail (if the user provided parameters are not valid). If I were on java, I would simply throw an exception. But on scala (although the same thing is possible), it is suggested to not use exceptions.

I perfectly know how to use Option or Try, but they all only make sense if you have something valid to return.

For example, think of a (imaginary) addPrintJob(printJob: printJob): Unit command which adds a print job to a printer. The job definition could now be invalid and the user should be notified of this.

I see the following two alternatives:

  1. Use exceptions anyway
  2. Return something from the method (like a "print job identifier") and then return a Option/Either/Try of that type. But this means adding a return value just for the sake of error handling.

What are the best practices here?


You are too deep into FP :-) You want to know whether the method is successful or not - return a Boolean!

Functions 2: Void (NonValue-Returning) Functions, name and the argument list as a statement in the program. First, if a function has a non-void return type, it must return a value of that type (using a return statement). Failure to do so will result in undefined behavior. The only exception to this rule is for function main(), which will assume a return value of 0 if one is not explicitly provided.


According to this Throwing exceptions in Scala, what is the "official rule" Throwing exceptions in scala is not advised as because it breaks the control flow. In my opinion you should throw an exception in scala only when something significant has gone wrong and normal flow should not be continued.

For all other cases it generally better to return the status/result of the operation that was performed. scala Option and Either serve this purpose. imho A function which does not return any value is a bad practice.

For the given example of the addPrintJob I would return an job identifier (as suggested by @marstran in comments), if this is not possible the status of addPrintJob.

How do you exit from a void function in C++?, What happens if you forget the return statement in a non void function? Void functions are “void” due to the fact that they are not supposed to return values. True, but not completely. We cannot return values but there is something we can surely return from void functions. Some of cases are listed below. A void function can do return We can simply write return statement in a void fun().


The problem is that usually when you have to model things for a specific method it is not about having success or failure ( true or false ) or ( 0 or 1 - Unit exit codes wise ) or ( 0 or 1 - true or false interpolation wise ) , but about returning status info and a msg , thus the most simplest technique I use ( whenever code review naysayers/dickheads/besserwissers are not around ) is that

val msg = "unknown error has occurred during ..."
val ret = 1 // defined in the beginning of the method, means "unknown error"
.... // action 
ret = 0 // when you finally succeeded to implement FULLY what THIS method was supposed to to
msg = "" // you could say something like ok , but usually end-users are not interested in your ok msgs , they want the stuff to work ...

at the end always return a tuple

return ( ret , msg ) 

or if you have a data as well ( lets say a spark data frame )

return ( ret , msg , Some(df))

Using return is more obvious, although not required ( for the purists ) ... Now because ret is just a stupid int, you could quickly turn more complex status codes into more complex Enums , objects or whatnot , but the point is that you should not introduce more complexity than it is needed into your code in the beginning , let it grow organically ...

and of course the caller would call like

 ( ret , msg , mayBeDf ) = myFancyFunc(someparam, etc)

Thus exceptions would mean truly error situations and you will avoid messy try catch jungles ... I know this answer WILL GET down-voted , because well there are too much guys from universities with however bright resumes writing whatever brilliant algos and stuff ending-up into the spagetti code we all are sick of and not something as simple as possible but not simpler and of course something that WORKS.

BUT, if you need only ok/nok control flow and chaining, here is bit more elaborated ok,nok example, which does really throw exception, which of course you would have to trap on an upper level , which works for spark:

  /**
    * a not so fancy way of failing asap, on first failing link in the control chain
    * @return true if valid, false if not
    */
  def isValid(): Boolean = {

    val lst = List(
      isValidForEmptyDF() _,
      isValidForFoo() _,
      isValidForBar() _
    )

    !lst.exists(!_()) // and fail asap ...
  }

  def isValidForEmptyDF()(): Boolean = {
    val specsAreMatched: Boolean = true

    try {
      if (df.rdd.isEmpty) {
        msg = "the file: " + uri + " is empty"
        !specsAreMatched
      } else {
        specsAreMatched
      }
    } catch {
      case jle: java.lang.UnsupportedOperationException => {
        msg = msg + jle.getMessage
        return false
      }
      case e: Exception => {
        msg = msg + e.getMessage()
        return false
      }
    }

  }

Disclaimer: my colleague helped me with the fancy functions syntax ...

Chapter 7 Functions, Void functions are created and used just like value-returning functions except they do result rather than modify object through reference argument (value-​returning Specify default parameter values when function name appears for first time  A good utilization of a void function would be to print a header/footer to a screen or file. Remember: there are two kinds of subprograms that the C++ language utilizes: value-returning functions and void functions. Both value-returning functions and void functions receive values through their parameter lists.


2.2, But a method failing to fulfill its purpose is an exceptional case (or at In this case, the caller needs a try-catch block anyways, but then he can better write: try { result = tryMyAPICall(); // business logic // will only reach this line  A function can have zero to many parameters, and it can return this many values. only one EXIT_FAILURE and ________ are named constants that may be used to indicate success or failure when the exit() function is called.


Function returning true/false vs. void when succeeding and throwing , In the case of a bool return value to indicate the success or failure of a method, it is expected to return the result of that calculation, the return type is obvious. A (n) ________ argument is passed to a parameter when the actual argument is left out of the function call. A function ________ eliminates the need to place a function definition before all calls to the function. You just studied 26 terms! Now up your study game with Learn mode. A (n) ________ argument is passed to a parameter when the actual


When and why you should use void (instead of e.g. bool/int , In-fact it is considered a good practice (for readability of code) to write return; statement to indicate end of function. filter_none. edit close. play_arrow. link @Lock Yes, exceptions can be caught and ignored. The primary difference is that the person calling the method with a return code needs to write code to handle success or failure, while the person calling the method needs to do nothing. If it works the next line will be executed but if it fails the exception continues.