<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def erase(self):
 """Erases the flash contents of the device. This erases the flash memory of the target device. If this method fails, the device may be left in an inoperable state. Args: self (JLink):
 the ``JLink`` instance Returns: Number of bytes erased. """

        try:
            # This has to be in a try-catch, as the device may not be in a
            # state where it can halt, but we still want to try and erase.
            if not self.halted():
                self.halt()
        except errors.JLinkException:
            # Can't halt, so just continue to erasing.
            pass

        res = self._dll.JLINK_EraseChip()
        if res < 0:
            raise errors.JLinkEraseException(res)

        return res