How should I handle errors within a plugin?

Building my first serious attempt of a WordPress plugin. I’m not sure how I should handle errors. I really just want to give the user some kind of message that the variable is wrong instead of throwring an error that makes the whole site not to work.

Inside a class it looks like this:

public function set_selectedcurrencies($currencies = null) {
    if ($currencies === null) {
        throw new Exception('No default currencies set');
    }
    if (!is_array($currencies)) {
        throw new Exception('Currencies must be an array existing of two default currencies, default from and default to');
    }
    $this->last_currencies = $currencies;
 }

If you then do like this:

$foo = new Foo();
$foo->set_selectedcurrencies(234);

The whole site would not work because an exception (currencies must be an array existing of two default currencies, default from and default to) is thrown and I don’t want that.

So how should I handle this?

UPDATE

I don’t want the “end user” to have to do something like this for my error-handling to work:

try {
$foo->set_selectedcurrencies( 5345 );
}
catch(Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

Would it be better just to return an error message from the function like this and let the programmer that uses the plugin to do something about it?

public function set_selectedcurrencies($currencies = null) {
        if ($currencies === null) {
            return 'No default currencies set';
        }
        if (!is_array($currencies)) {
            return 'Currencies must be an array existing of two default currencies, default from and default to';
        }
        $this->last_currencies = $currencies;
     }

UPDATE2:

I’ve figured out what I was looking for. Maybe my question was misleading somehow, but it would be ok to show messages only in admin – section because this error should only be for a guidance for the programmer that implements the plugin.

I’ve found this: https://codex.wordpress.org/Plugin_API/Action_Reference/admin_notices and made it into “class-style“:

class FOO {

private $errormessage;

public function error_notice() {
    $message = $this->errormessage;
    echo"<div class=\"error\"> <p>$message</p></div>"; 
}

public function set_selectedcurrencies($currencies = null) {
    if ($currencies === null) {
        $this->errormessage = 'No default currencies set';
        add_action( 'admin_notices', array($this,'error_notice' ) ); 
        return;
    }

    if (!is_array($currencies)) {
        $this->errormessage = 'Currencies must be an array existing of two default currencies, default from and default to';
        add_action( 'admin_notices', array($this,'error_notice' ) ); 
        return;
    }

    //default currencies in format currency1, currency2 (USD, EUR)
    $this->last_currencies = $currencies;
}

}

I hope this helps anyboy/someone that stumble upon the same issue!

Solutions Collecting From Web of "How should I handle errors within a plugin?"