Can I try/catch a warning?
Set and restore error handler
One possibility is to set your own error handler before the call and restore the previous error handler later with restore_error_handler()
.
set_error_handler(function() { /* ignore errors */ });
dns_get_record();
restore_error_handler();
You could build on this idea and write a re-usable error handler that logs the errors for you.
set_error_handler([$logger, 'onSilencedError']);
dns_get_record();
restore_error_handler();
Turning errors into exceptions
You can use set_error_handler()
and the ErrorException
class to turn all php errors into exceptions.
set_error_handler(function($errno, $errstr, $errfile, $errline) {
// error was suppressed with the @-operator
if (0 === error_reporting()) {
return false;
}
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
});
try {
dns_get_record();
} catch (ErrorException $e) {
// ...
}
The important thing to note when using your own error handler is that it will bypass the error_reporting
setting and pass all errors (notices, warnings, etc.) to your error handler. You can set a second argument on set_error_handler()
to define which error types you want to receive, or access the current setting using ... = error_reporting()
inside the error handler.
Suppressing the warning
Another possibility is to suppress the call with the @ operator and check the return value of dns_get_record()
afterwards. But I'd advise against this as errors/warnings are triggered to be handled, not to be suppressed.
How to correctly log warnings and errors using `tryCatch` in R?
Based on Ronak's helpful comment and the following question How do I save warnings and errors as output from a function?, the code can be simplified as follows:
# Storage.
warns = list()
errs = list()
# Example function.
fun <- function(i) {
# Issue warning.
warning(paste("Warn.", i))
# Stop.
if(i == 3) { stop(paste("Err.", i)) }
}
# Evaluate `fun`.
for (i in 1:4) {
tryCatch(withCallingHandlers(
expr = fun(i),
# Handle the warnings.
warning = function(w) {
warns <<- c(warns, list(w))
invokeRestart("muffleWarning")
}),
# Handle the errors.
error = function(e) {
errs <<- c(errs, list(e))
}
)
}
The output then looks like:
warns
# [[1]]
# <simpleWarning in fun(i): Warn. 1>
#
# [[2]]
# <simpleWarning in fun(i): Warn. 2>
#
# [[3]]
# <simpleWarning in fun(i): Warn. 3>
#
# [[4]]
# <simpleWarning in fun(i): Warn. 4>
errs
# [[1]]
# <simpleError in fun(i): Err. 3>
More information and links are provided in the question linked above.
How to write trycatch in R
Well then: welcome to the R world ;-)
Here you go
Setting up the code
urls <- c(
"http://stat.ethz.ch/R-manual/R-devel/library/base/html/connections.html",
"http://en.wikipedia.org/wiki/Xz",
"xxxxx"
)
readUrl <- function(url) {
out <- tryCatch(
{
# Just to highlight: if you want to use more than one
# R expression in the "try" part then you'll have to
# use curly brackets.
# 'tryCatch()' will return the last evaluated expression
# in case the "try" part was completed successfully
message("This is the 'try' part")
readLines(con=url, warn=FALSE)
# The return value of `readLines()` is the actual value
# that will be returned in case there is no condition
# (e.g. warning or error).
# You don't need to state the return value via `return()` as code
# in the "try" part is not wrapped inside a function (unlike that
# for the condition handlers for warnings and error below)
},
error=function(cond) {
message(paste("URL does not seem to exist:", url))
message("Here's the original error message:")
message(cond)
# Choose a return value in case of error
return(NA)
},
warning=function(cond) {
message(paste("URL caused a warning:", url))
message("Here's the original warning message:")
message(cond)
# Choose a return value in case of warning
return(NULL)
},
finally={
# NOTE:
# Here goes everything that should be executed at the end,
# regardless of success or error.
# If you want more than one expression to be executed, then you
# need to wrap them in curly brackets ({...}); otherwise you could
# just have written 'finally=<expression>'
message(paste("Processed URL:", url))
message("Some other message at the end")
}
)
return(out)
}
Applying the code
> y <- lapply(urls, readUrl)
Processed URL: http://stat.ethz.ch/R-manual/R-devel/library/base/html/connections.html
Some other message at the end
Processed URL: http://en.wikipedia.org/wiki/Xz
Some other message at the end
URL does not seem to exist: xxxxx
Here's the original error message:
cannot open the connection
Processed URL: xxxxx
Some other message at the end
Warning message:
In file(con, "r") : cannot open file 'xxxxx': No such file or directory
Investigating the output
> head(y[[1]])
[1] "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"
[2] "<html><head><title>R: Functions to Manipulate Connections</title>"
[3] "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">"
[4] "<link rel=\"stylesheet\" type=\"text/css\" href=\"R.css\">"
[5] "</head><body>"
[6] ""
> length(y)
[1] 3
> y[[3]]
[1] NA
Additional remarks
tryCatch
tryCatch
returns the value associated to executing expr
unless there's an error or a warning. In this case, specific return values (see return(NA)
above) can be specified by supplying a respective handler function (see arguments error
and warning
in ?tryCatch
). These can be functions that already exist, but you can also define them within tryCatch()
(as I did above).
The implications of choosing specific return values of the handler functions
As we've specified that NA
should be returned in case of error, the third element in y
is NA
. If we'd have chosen NULL
to be the return value, the length of y
would just have been 2
instead of 3
as lapply()
will simply "ignore" return values that are NULL
. Also note that if you don't specify an explicit return value via return()
, the handler functions will return NULL
(i.e. in case of an error or a warning condition).
"Undesired" warning message
As warn=FALSE
doesn't seem to have any effect, an alternative way to suppress the warning (which in this case isn't really of interest) is to use
suppressWarnings(readLines(con=url))
instead of
readLines(con=url, warn=FALSE)
Multiple expressions
Note that you can also place multiple expressions in the "actual expressions part" (argument expr
of tryCatch()
) if you wrap them in curly brackets (just like I illustrated in the finally
part).
Store errors and warnings with tryCatch() in a list
Here's a version that puts the tryCatch
within the loop, and saves the errors and warnings that were generated:
hospitals_url <- "https://services1.arcgis.com/Hp6G80Pky0om7QvQ/arcgis/rest/services/Hospitals_1/FeatureServer/0/query?where=1%3D1&outFields=*&outSR=4326&f=json"
hospitals_num <- c(0, 2000, 4000, 6000)
hosp_e <- list()
hosp_w <- list()
hosget <- lapply(hospitals_num, function(num) {
tryCatch({
hospitals_url_loop <- paste(hospitals_url, "&resultOffset=", num)
hospitals_json <- fromJSON(hospitals_url_loop)
hospitals_df <- as.data.frame(hospitals_json$features$attributes)},
error = function(e){
hosp_e <<- c(hosp_e, list(e))
print(e)},
warning = function(w){
hosp_w <<- c(hosp_w, list(w))
print(w)}
) })
hospitals_df <- do.call(rbind, hosget)
I didn't adapt your finally
code to this rearrangement; I'll leave that to you. But at the end, hosp_e
will be a list holding all the errors, and hosp_w
will be a list holding all the warnings.
In Python, how does one catch warnings as if they were exceptions?
To handle warnings as errors simply use this:
import warnings
warnings.filterwarnings("error")
After this you will be able to catch warnings same as errors, e.g. this will work:
try:
some_heavy_calculations()
except RuntimeWarning:
breakpoint()
P.S. Added this answer because the best answer in comments contains misspelling: filterwarnigns
instead of filterwarnings
.
How to get rid of IntelliJ warnings/errors on try/catch block's IOException catch?
You can find the corresponding inspection at Settings > Editor > Inspections > Java > Error Handling > Overly broad 'catch' block
. I would like to note that this inspection was not enabled by default for me.
There are a couple ways to "fix" the warning.
Use Multi-Catch
@Override
public void readFile(File file) {
try {
...
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
Modify Inspection
This is probably the preferred option, especially if you want to avoid disabling the inspection completely. From the description of the inspection:
Reports catch blocks which have parameters which are more generic than the exceptions thrown by the corresponding try block.
Use the first checkbox below to have this inspection only warn on the most generic exceptions.
Use the second checkbox below to ignore any exceptions which hide other exceptions, but which may be thrown and thus are technically not overly broad.
The last paragraph is what we're interested in.
Is it possible to capture all warnings raised in a try block in python?
Were you expecting something like the following:
import warnings
warnings.filterwarnings('error')
def warning_func():
print('hello')
warnings.warn(Warning('Warn1'))
print('hi')
warnings.warn(Warning('Warn2'))
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
warning_func()
print(w)
Related Topics
Best Way to Use PHP to Encrypt and Decrypt Passwords
Reference: What Is a Perfect Code Sample Using the MySQL Extension
How to Remove Duplicate Values from an Array in PHP
PHP MySQLi_Connect: Authentication Method Unknown to the Client [Caching_Sha2_Password]
How to Send a Firebase Cloud Messaging Notification Without Use the Firebase Console
Send JavaScript Variable to PHP Variable
Comparing String to Integer Gives Strange Results
How to Convert a Pdf Document to a Preview Image in PHP
Create a CSV File For a User in PHP
How to Generate Xml File Dynamically Using PHP
Convert a PHP Object to an Associative Array
Htmlentities() Vs. Htmlspecialchars()
What's Wrong With Using $_Request[]
Htaccess: Add/Remove Trailing Slash from Url