[SOLVED] Related to exception checking (IOException) in Java. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

[SOLVED] Related to exception checking (IOException) in Java.

https://code.sololearn.com/cweTji2REm4f/?ref=app I don't get why this code is generating an error. Please help me. I'll be so grateful.

31st Jan 2020, 6:08 PM
Abdul S Ansari
Abdul S Ansari - avatar
11 Answers
+ 6
Did not go through the whole code but just to remind you that IOException comes under checked exception which should be handled at all times. You can do that by either using the throws clause following the main method like- throws IOException OR Put the code inside main method into a try block and followed by- catch(IOException a) { System.out.println("Exception caught"); }
31st Jan 2020, 6:35 PM
Avinesh
Avinesh - avatar
+ 4
there is interchanged alphabet and inp // if(!(inp.contains(Character.toString(alphabets.charAt(i))))){ //.. try { // need to catch an exception from vigenere() vigenere(inp, key); } catch(IllegalArgumentException e) { System.out.println(e.getMessage() ); } } // end of main() public static void vigenere(String inp, String key) throws IllegalArgumentException { //function to encrypt by vigenere vipher. String alphabets = "abcdefghijklmnopqrstuvwxyz"; for(int i=0; i<inp.length(); i++){ // changed here <<<< // if(!(inp .contains(Character.toString(alphabets.charAt(i))))){ if(!(alphabets.contains(Character.toString(inp .charAt(i))))){ throw new IllegalArgumentException("input must not contain any numbers or special characters."); }
31st Jan 2020, 11:35 PM
zemiak
+ 3
I am wondering why your vignere() method throws an IOException. Your are not working with files or sockets or something like this. https://stackoverflow.com/questions/13216148/java-what-throws-an-ioexception
31st Jan 2020, 10:42 PM
Denise Roßberg
Denise Roßberg - avatar
+ 2
Denise Roßberg That can be done, or probably create a separate method which checks whether the input meets all the necessary criteria. Or do a simple check in the main() itself. We can avoid exceptions this way.
1st Feb 2020, 1:32 PM
Avinesh
Avinesh - avatar
+ 2
zemiak Denise Roßberg Thank you for all of your assistance. I understand it much better now then before.
1st Feb 2020, 4:09 PM
Abdul S Ansari
Abdul S Ansari - avatar
+ 2
1st Feb 2020, 4:13 PM
Denise Roßberg
Denise Roßberg - avatar
+ 2
Well, now I'm planning to try a different approach. I'll create a segregate class- Vigenere, and in it I'll make two methods- encrypt and decrypt.
1st Feb 2020, 4:26 PM
Abdul S Ansari
Abdul S Ansari - avatar
+ 1
Avinesh Thank you very much
1st Feb 2020, 5:48 AM
Abdul S Ansari
Abdul S Ansari - avatar
+ 1
use IllegalArgumentException, it is not checked and you do not have to catch it, but then you will not get a message from it.
1st Feb 2020, 7:08 AM
zemiak
+ 1
Abdul Samad I wouldn't throw an exception at all. The method could return the string without modification. In my opinion, the program that implements vignere () should ensure that the user cannot make incorrect entries. Avinesh zemiak What do you think about it?
1st Feb 2020, 12:35 PM
Denise Roßberg
Denise Roßberg - avatar
0
Denise Roßberg Because I've designed it to do so. So should I make any method to throw IOException only when it is dealing with files or sockets. My vigenere () method takes two arguments which mustn't contain any special character or number. This sounds like a fault in the parameters (which are the inputs) given to the method, so I made it to throw IOException. Should I change it to throw some other kind of excpetion? If yes then please tell me which one, Sir. I'm a beginner in Java.
1st Feb 2020, 5:57 AM
Abdul S Ansari
Abdul S Ansari - avatar