java - exception handling

also ich versuche konsolen eingabe mir der scanner klasse abzufangen und diese wirft ja eine FileNotFoundException d.h. ich brauche einen try catch block ...

Code:
try {
	   
	   Scanner sc = new Scanner ( System.in );
	   
	   System.out.print ("\nSource File: ");
		
	   srcfile = sc.nextLine();
	   
	   System.out.print ("\nDestination File: ");
	   
	   destfile = sc.nextLine();
	   
	   System.out.print ("\nEncryption Key: ");
	   
	   key = sc.nextInt();
	   
	}
	 
	 catch ( FileNotFoundException e ) {
	 
	 
	 System.err.println ("Error: " + e.getMessage());}
	 
	}
aber der compiler hat ein problem mit der FileNotFoundException

fehler:


Code:
Main.java:38: exception java.io.FileNotFoundException is never thrown in body of corresponding try state
ment
         catch ( FileNotFoundException e ) {
         ^
1 error

wenn ich einfach nur Exception im catch block abfange klappts wunderbar o0
 
Schau mal in der API nach, was bei Scanner genau eine FileNotFoundException wirft, nämlcih nur die Konstruktoren:
public Scanner(File source)
public Scanner(File source, String charsetName)


du benutzt aber den Konstruktor

public Scanner(InputStream source)

welcher diese Exception nicht wirft, also brauachst du sie nicht abzufangen.

Mfg ThePhil
 
oh stimmt das habe ich wohl übersehen :(
aber

aber sehr komisch war , ich habe mal den code mit "Exception" komplieiert das hat geklappt, danach habe ich Exception durch FileNotFoundException ersetzt und dann hat es funktioniert ...
aber nach einer neukompilierung kam wieder der fehler also mal klappts und mal nicht o0
das gleiche hatte ich auch bei diesem beispiel:
Code:
public void setPw ( File data ) {
  	
 
  this.pw = new PrintWriter ( data ); 
 
 }

mal will der compiler einen try catch block mit FileNotFound Exception und mal nicht , klingt komisch ist aber so :x
obwohl der PrintWriter konstruktor gar keine exception wirft -.-
 
Zurück
Oben