D. Posey D. Posey - 1 month ago 10
Java Question

Program reports exceptions when not using try and catch

I'm still very new to java and I'm working on a school assignment that's working on cryptography. When I was searching for methods to read from files I saw many had a try and catch block. I'm not very familiar with the use of these and I want to try and avoid using them in my code but when I remove them I have two exceptions reported at the new in

new FileReader
and at the bracket after
reader.readLine()
. But if I do use them it works relatively well. Can anyone explain what is happening? Also when using the catch and try I get an exception Null when my encoding is done. Any help is appreciated.

import java.io.*;
import java.util.*;

public class Encrypter {

public static void main(String[] args) {

File input = null;
if (1 < args.length) {
input = new File(args[1]);
} else {
System.err.println("Invalid arguments count:" + args.length);
System.exit(0);
}
String key = args[0];
BufferedReader reader = null;
try {
int i = 0;
String[] inputText = new String[20];
String[] encryptText = new String[20];
reader = new BufferedReader(new FileReader(input));
while ((inputText[i] = reader.readLine()) != null) {
encryptText[i] = inputText[i];
System.out.println(inputText[i]);
++i;
}
int hash = key.hashCode();
Random random = new Random(hash);
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
String alphabetPerm = alphabet;
char temp;
for (int j = 0; j < 100; j++) {
int n1 = random.nextInt(27) + 0;
int n2 = random.nextInt(27) + 0;
char[] swapper = alphabet.toCharArray();
temp = swapper[n1];
swapper[n1] = swapper[n2];
swapper[n2] = temp;
String alphaSwap = new String(swapper);
alphabet = alphaSwap;
}
System.out.println(alphabet);
for (int k = 0; k < inputText.length; k++) {
encryptText[k] = inputText[k].replaceAll("[^A-Za-z0-9 ]+", " ");
for (int j = 0; j < inputText[k].length(); j++) {
int index = alphabetPerm.indexOf(encryptText[k].charAt(j));
encryptText[k] = alphabetSwapper(encryptText[k], alphabet, index, j);
System.out.println(encryptText[k]);
}
}
} catch (Exception e) {
System.err.println("Caught Exception: " + e.getMessage());
}
}

public static String alphabetSwapper(String s, String alpha, int index, int value) {
char toSwap = s.charAt(value);
char[] inputChars = s.toCharArray();
inputChars[value] = alpha.charAt(index);
String swapped = new String(inputChars);
return swapped;
}
}

Answer

You are better off not catching the exception the way you are because you are discarding the two most useful pieces of information, which exception was thrown and where did it happen.

Instead of catching the exception, you can add the exceptions to the throws clause of main()

e.g.

public static void main(String[] args) throws IOException {
    // ...
}

Can anyone explain what is happening?

When you read a file you might be an IOException e.g. if the file is not present. You might have to catch this exception but for now you can let the caller to main print it out if it happens.

Also when using the catch and try I get an exception Null when my encoding is done

This means you are triggering an exception without a message. If you print the exception and where it happened(or let the JVM do it for you) you will see where.

To determine why this is happening I suggest stepping through the code in your debugger so you can understand what each line does.

Comments