# Replacing warning message with custom text

I am using the following code to determine the maximum of a function:

f[x_] = Log[3/10, 3 - 2 x - x^2]
xr = FunctionDomain[f[x], x]
Maximize[{f[x], xr}, x]


As the maximum value does not exist, a warning message is returned:

Maximize::natt: The maximum is not attained at any point satisfying the given constraints.

I want to modify the code such that it doesn't give a warning message, but prints

The function does not have a maximum value in this interval

when the maximum does not exist, and give the maximum otherwise.

## 1 Answer

Using Check:

Clear["Global*"]
f[x_] = Log[3/10, 3 - 2 x - x^2]
xr = FunctionDomain[f[x], x]
FunctionRange[f[x], x, y]
Quiet@Check[Maximize[{f[x], xr}, x],
"The function does not have a maximum value over this interval",
Maximize::natt]


You can also specify a list of error messages.

• Clear["Global*"] f[x_] = Log[3/10, 3 + 2 x + x^2] xr = FunctionDomain[f[x], x] FunctionRange[f[x], x, y] Quiet@Check[Maximize[{f[x], xr}, x], "The function does not have a maximum value over this interval", Maximize::natt] Quiet@Check[Minimize[{f[x], xr}, x], "The function does not have a minimum value over this interval", Minimize::natt] Plot[f[x], {x, -5, 5}] Aug 14, 2023 at 14:16