[SalesForce] Type cannot be constructed: Exception

In Apex I have this:

...
if (forceException) {
   Integer a = 1/0;
}
...

But after I ran SFDX Scanner I got an error that variable a is defined but not used (UnusedLocalVariable) so I decided to do next thing:

...
if (forceException) {
   throw new Exception('Exception');
}
...

but now I had problem because pipeline throw me next error:

Type cannot be constructed: Exception

does anyone know is there any other option what I can use in if statement instead of throw new Exception('Exception'); and Integer a = 1/0;?

Best Answer

You can create a custom Exception class, and throw an instance of that. For example:

class MyOwnException extends Exception{}

and then you can throw the custom exception.

if (forceException) {
   throw new MyOwnException('Exception');
}