Is there any way to make an exception when fwrite cannot write?

Asked

Viewed 61 times

4

I usually use the class SplFileObject to be able to work with files. The same does not happen with sockets, when necessary to connect, it is necessary to use the function fsockopen or stream_socket_client.

I need that, in a particular code snippet, in case the fwrite cannot write the data, throw an exception.

Is there any way to do that?

  • Which error of? a fatal error?

  • @rray, specifically cannot write the data. That is, some kind of connection error, or delay. I don’t know how to say exactly, but I think of an error when writing data to a socket connection

  • Have you tried using the set_error_handler, when an error occurs you capture it and launches an Exception.

  • I wanted to capture the specific error of a fwrite

  • That would be http://php.net/manual/en/function.fwrite.php#81269 ?

  • fwrite() === FALSE? I wonder if we could make one fwrite($data, 'teste') or $this->callException()? I’m sure you won’t accept a throw after the OR

  • Specific error type, with details of the occurred ?

  • No need to be specific. The fwrite just need not be able to write on the current connection.

  • He still has doubts about the subject?

Show 4 more comments

1 answer

2


I could do something like this:

> $resultado = fwrite('log.txt'); 
> if (false === $resultado) 
> {
>   throw new RuntimeException('Erro Especifico'); 
> }
  • Right. It is worth remembering that, for some functions, it is a good idea to put @ before, not to issue annoying warnings in the middle of your code :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.