Fórum Error: unreported exception exception; must be caught or declared to be thrown #562005
08/09/2016
0
Bom dia.
Estou tendo certas dificuldade em realizar a criptografia de meu arquivo.
Pesquisei na internet e em alguns outros foruns e estavam dizendo que necessitava estar dentro de um bloco try catch para funcionar. porém mesmo realizando este metodo da o seguinte erro: error: unreported exception Exception; must be caught or declared to be thrown.
Estou tendo certas dificuldade em realizar a criptografia de meu arquivo.
Pesquisei na internet e em alguns outros foruns e estavam dizendo que necessitava estar dentro de um bloco try catch para funcionar. porém mesmo realizando este metodo da o seguinte erro: error: unreported exception Exception; must be caught or declared to be thrown.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | Segue o meu codigo: import java.io.*; import java.io.File; import java.io.FileNotFoundException; import java.lang.IllegalStateException; import java.util.NoSuchElementException; import java.util.Scanner; public class CryptoDummy { private static byte[] textoCifrado; private static byte[] textoDecifrado; static String sMsgClara; static String sMsgCifrada = null; static String sMsgDecifrada = null; static byte[] bMsgClara = null; static byte[] bMsgCifrada = null; static byte[] bMsgDecifrada = null; public CryptoDummy() { textoCifrado = null; textoDecifrado = null; sMsgClara = pegaTexto(); } public static void geraChave(File fDummy) throws IOException { // Gera uma chave Dummy simetrica (dk: 0 a 100): int dk = (int) (Math.random()*101); // Grava a chave Dummy simetrica em formato serializado ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fDummy)); oos.writeObject(dk); oos.close(); } public static void geraCifra(byte[] texto, File fDummy) throws IOException, ClassNotFoundException { ObjectInputStream ois = new ObjectInputStream (new FileInputStream (fDummy)); int iDummy = (Integer) ois.readObject(); ois.close(); textoCifrado = texto; for(int i = 0; i < texto.length; i++) { textoCifrado = (byte) (textoCifrado + i + iDummy); } } public static byte[] getTextoCifrado() throws Exception { return textoCifrado; } public void geraDecifra(byte[] texto, File fDummy) throws IOException, ClassNotFoundException { ObjectInputStream ois = new ObjectInputStream (new FileInputStream (fDummy)); int iDummy = (Integer) ois.readObject(); ois.close(); textoDecifrado = texto; for(int i = 0; i < texto.length; i++) { textoDecifrado = (byte) (textoDecifrado - i - iDummy); } } public byte[] getTextoDecifrado() throws Exception { return textoDecifrado; } public static void criptografa() throws Exception { try{ // Instancia um objeto da classe CryptoDummy //CryptoDummy cdummy = new CryptoDummy(); // Gera a chave criptografica Dummy simetrica e nome do arquivo onde sera armazenada geraChave(new File ("login.txt")); // Gera a cifra Dummy da mensagem dada, com a chave Dummy simetrica dada geraCifra(bMsgClara, new File ("chave.dummy")); // Recebe o texto cifrado bMsgCifrada = getTextoCifrado(); // Converte o texto byte[] no equivalente String sMsgCifrada = (new String (bMsgCifrada, "ISO-8859-1")); } catch ( NoSuchElementException elementException ) { System.err.println( "File improperly formed." ); System.exit( 1 ); } // end catch catch ( IllegalStateException stateException ) { System.err.println( "Error reading from file." ); System.exit( 1 ); } // end catch } public void descriptografa() throws Exception { try { // Gera a decifra Dummy da mensagem dada, segundo a chave Dummy simetrica gerada geraDecifra(bMsgCifrada, new File ("chave.dummy")); // recebe o texto decifrado bMsgDecifrada = getTextoDecifrado(); // Converte o texto byte[] no equivalente String sMsgDecifrada = (new String (bMsgDecifrada, "ISO-8859-1")); } catch ( NoSuchElementException elementException ) { System.err.println( "File improperly formed." ); System.exit( 1 ); } // end catch catch ( IllegalStateException stateException ) { System.err.println( "Error reading from file." ); System.exit( 1 ); } // end catch } public String pegaTexto() { String line = ""; Scanner in = new Scanner(System.in); try { in = new Scanner(new File("login.txt")); } // end try catch( FileNotFoundException fileNotFoundException ) { System.err.println( "Error opening file." ); System.exit( 1 ); } // end catch try { while (in.hasNext()) { in.nextLine(); //System.out.println(line); } } catch ( NoSuchElementException elementException ) { System.err.println( "File improperly formed." ); in.close(); System.exit( 1 ); } // end catch catch ( IllegalStateException stateException ) { System.err.println( "Error reading from file." ); System.exit( 1 ); } // end catch //System.out.println(line); return line ; } } E quando chamo esse método em outra classe da a seguinte mensagem: error: unreported exception Exception; must be caught or declared to be thrown |

Marcoa
Curtir tópico
+ 0
Responder
Posts
09/09/2016
Jones Granatyr
Olá,
Cada tipo de exceção precisa ser tratada no código. Para resolver rápido você pode colocar o seguinte no método que está dando o erro: throws Exception. Assim ele "pegará" todos os tipos de exceção, porém, sem um tratamento mais detalhado de cada uma.
Jones
Cada tipo de exceção precisa ser tratada no código. Para resolver rápido você pode colocar o seguinte no método que está dando o erro: throws Exception. Assim ele "pegará" todos os tipos de exceção, porém, sem um tratamento mais detalhado de cada uma.
Jones
Responder
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)