Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Should I handle InputMismatches?

$
0
0
My teacher and textbook say runtime exceptions should not be handled because they're usually bugs in our code that we should fix.

But what if I'm parsing a String inputted from a user into an Integer and the dumb user decides to put letter characters in instead? InputMismatchExceptions are RunTime Exceptions right?

Or should I create my own Exception to handle it?

I think it should just be wrapped in a try/catch block. My teacher is probably talking about nullPointers or outofbounds exceptions.

Just wanted to double check though.


EDIT:

Hmm...I tried a try/catch block for InputMismatchException but when the I ran it, the catch block didn't "catch" it.

try{
				Double.parseDouble(tempList[3]);
			}
			catch(InputMismatchException ex){
				System.err.println("Fourth list item contained null or non-number characters.");
				System.err.println("Fourth element automatically set to 0");
				tempList[3] = "0";
			}



Can it not catch runtime exceptions?

It works if I replace "MismatchException" with just "Exception".

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>