diff --git a/README.md b/README.md
index a9d19bf..983b7d1 100644
--- a/README.md
+++ b/README.md
@@ -163,7 +163,7 @@ The format is not formally specified and still improves over time.  That being s
 
 Keys can be unquoted or single-quoted. Values can be unquoted, single- or double-quoted.
 Spaces before and after keys, equal signs, and values are ignored. Values can be followed
-by a comment.  Lines can start with the `export` directive, which has no effect on their
+by a comment.  Lines can start with the `export` directive, which does not affect their
 interpretation.
 
 Allowed escape sequences:
diff --git a/src/dotenv/main.py b/src/dotenv/main.py
index 05d377a..3321788 100644
--- a/src/dotenv/main.py
+++ b/src/dotenv/main.py
@@ -125,15 +125,16 @@ def rewrite(
     path: Union[str, os.PathLike],
     encoding: Optional[str],
 ) -> Iterator[Tuple[IO[str], IO[str]]]:
+    dest = None
     try:
         if not os.path.isfile(path):
             with open(path, "w+", encoding=encoding) as source:
                 source.write("")
-        with tempfile.NamedTemporaryFile(mode="w+", delete=False, encoding=encoding) as dest:
-            with open(path, encoding=encoding) as source:
-                yield (source, dest)  # type: ignore
+        dest = tempfile.NamedTemporaryFile(mode="w+", delete=False, encoding=encoding)
+        with open(path, encoding=encoding) as source:
+            yield (source, dest)  # type: ignore
     except BaseException:
-        if os.path.isfile(dest.name):
+        if dest and os.path.isfile(dest.name):
             os.unlink(dest.name)
         raise
     else:

diff --git a/tests/test_main.py b/tests/test_main.py
index 82c73ba..84a982f 100644
--- a/tests/test_main.py
+++ b/tests/test_main.py
@@ -22,6 +22,11 @@ def test_set_key_no_file(tmp_path):
     assert os.path.exists(nx_file)
 
 
+def test_set_key_invalid_file():
+    with pytest.raises(TypeError):
+        result = dotenv.set_key(None, "foo", "bar")
+
+
 @pytest.mark.parametrize(
     "before,key,value,expected,after",
     [

Error Handling in rewrite is incorrect.
Look at:
https://github.com/theskumar/python-dotenv/blob/master/src/dotenv/main.py#L136

If lines 136, 137 or 140 there are ever hit, an error "local variable 'dest' referenced before assignment" will be thrown, because the `dest` variable only exists within the scope of the `with` block above.
tests/test_main.py::test_set_key_invalid_file
tests/test_main.py::test_set_key_no_file tests/test_main.py::test_set_key[-a--expected0-a=''\n] tests/test_main.py::test_set_key[-a-b-expected1-a='b'\n] tests/test_main.py::test_set_key[-a-'b'-expected2-a='\\'b\\''\n] tests/test_main.py::test_set_key[-a-"b"-expected3-a='"b"'\n] tests/test_main.py::test_set_key[-a-b'c-expected4-a='b\\'c'\n] tests/test_main.py::test_set_key[-a-b"c-expected5-a='b"c'\n] tests/test_main.py::test_set_key[a=b-a-c-expected6-a='c'\n] tests/test_main.py::test_set_key[a=b\n-a-c-expected7-a='c'\n] tests/test_main.py::test_set_key[a=b\n\n-a-c-expected8-a='c'\n\n] tests/test_main.py::test_set_key[a=b\nc=d-a-e-expected9-a='e'\nc=d] tests/test_main.py::test_set_key[a=b\nc=d\ne=f-c-g-expected10-a=b\nc='g'\ne=f] tests/test_main.py::test_set_key[a=b\n-c-d-expected11-a=b\nc='d'\n] tests/test_main.py::test_set_key[a=b-c-d-expected12-a=b\nc='d'\n] tests/test_main.py::test_set_key_encoding tests/test_main.py::test_get_key_no_file tests/test_main.py::test_get_key_not_found tests/test_main.py::test_get_key_ok tests/test_main.py::test_get_key_encoding tests/test_main.py::test_get_key_none tests/test_main.py::test_unset_with_value tests/test_main.py::test_unset_no_value tests/test_main.py::test_unset_encoding tests/test_main.py::test_unset_non_existent_file tests/test_main.py::test_find_dotenv_no_file_raise tests/test_main.py::test_find_dotenv_no_file_no_raise tests/test_main.py::test_find_dotenv_found tests/test_main.py::test_load_dotenv_existing_file tests/test_main.py::test_load_dotenv_no_file_verbose tests/test_main.py::test_load_dotenv_existing_variable_no_override tests/test_main.py::test_load_dotenv_existing_variable_override tests/test_main.py::test_load_dotenv_redefine_var_used_in_file_no_override tests/test_main.py::test_load_dotenv_redefine_var_used_in_file_with_override tests/test_main.py::test_load_dotenv_string_io_utf_8 tests/test_main.py::test_load_dotenv_file_stream tests/test_main.py::test_load_dotenv_in_current_dir tests/test_main.py::test_dotenv_values_file tests/test_main.py::test_dotenv_values_string_io[env0-a=$b-False-expected0] tests/test_main.py::test_dotenv_values_string_io[env1-a=$b-True-expected1] tests/test_main.py::test_dotenv_values_string_io[env2-a=${b}-False-expected2] tests/test_main.py::test_dotenv_values_string_io[env3-a=${b}-True-expected3] tests/test_main.py::test_dotenv_values_string_io[env4-a=${b:-d}-False-expected4] tests/test_main.py::test_dotenv_values_string_io[env5-a=${b:-d}-True-expected5] tests/test_main.py::test_dotenv_values_string_io[env6-b=c\na=${b}-True-expected6] tests/test_main.py::test_dotenv_values_string_io[env7-a=${b}-True-expected7] tests/test_main.py::test_dotenv_values_string_io[env8-a=${b:-d}-True-expected8] tests/test_main.py::test_dotenv_values_string_io[env9-a="${b}"-True-expected9] tests/test_main.py::test_dotenv_values_string_io[env10-a='${b}'-True-expected10] tests/test_main.py::test_dotenv_values_string_io[env11-a=x${b}y-True-expected11] tests/test_main.py::test_dotenv_values_string_io[env12-a=${a}-True-expected12] tests/test_main.py::test_dotenv_values_string_io[env13-a=${a}-True-expected13] tests/test_main.py::test_dotenv_values_string_io[env14-a=${a:-c}-True-expected14] tests/test_main.py::test_dotenv_values_string_io[env15-a=${a:-c}-True-expected15] tests/test_main.py::test_dotenv_values_string_io[env16-a=${b}${b}-True-expected16] tests/test_main.py::test_dotenv_values_string_io[env17-b=d\na=${b}-True-expected17] tests/test_main.py::test_dotenv_values_string_io[env18-a=b\na=c\nd=${a}-True-expected18] tests/test_main.py::test_dotenv_values_string_io[env19-a=b\nc=${a}\nd=e\nc=${d}-True-expected19] tests/test_main.py::test_dotenv_values_file_stream