diff --git a/ccode/valaccodewriter.vala b/ccode/valaccodewriter.vala
index ff0677b..8e7c61a 100644
--- a/ccode/valaccodewriter.vala
+++ b/ccode/valaccodewriter.vala
@@ -51,7 +51,7 @@ public class Vala.CCodeWriter {
 	private string temp_filename;
 	private bool file_exists;
 
-	private FileStream stream;
+	private FileStream? stream;
 	
 	private int indent;
 	private int current_line_number = 1;
@@ -80,6 +80,11 @@ public class Vala.CCodeWriter {
 			stream = FileStream.open (filename, "w");
 		}
 
+		if (stream == null) {
+			warning ("Cannot open file '%s'\n", filename);
+			return false;
+		}
+
 		write_string ("/* %s generated by valac, the Vala compiler".printf (Path.get_basename (filename)));
 
 		// Write the file name if known
@@ -92,7 +97,7 @@ public class Vala.CCodeWriter {
 		write_newline ();
 		write_newline ();
 
-		return (stream != null);
+		return true;
 	}
 
 	/**
diff --git a/vala/valacodewriter.vala b/vala/valacodewriter.vala
index e6dd882..e818e72 100644
--- a/vala/valacodewriter.vala
+++ b/vala/valacodewriter.vala
@@ -70,18 +70,21 @@ public class Vala.CodeWriter : CodeVisitor {
 		this.context = context;
 	
 		stream = FileStream.open (filename, "w");
+		if (stream == null) {
+			warning ("Cannot open '%s'\n", filename);
+		} else {
+			write_string ("/* %s generated by %s, do not modify. */".printf (Path.get_basename (filename), Environment.get_prgname ()));
+			write_newline ();
+			write_newline ();
 
-		write_string ("/* %s generated by %s, do not modify. */".printf (Path.get_basename (filename), Environment.get_prgname ()));
-		write_newline ();
-		write_newline ();
-
-		current_scope = context.root.scope;
+			current_scope = context.root.scope;
 
-		context.accept (this);
+			context.accept (this);
 
-		current_scope = null;
+			current_scope = null;
 
-		stream = null;
+			stream = null;
+		}
 	}
 
 	public override void visit_namespace (Namespace ns) {
