java - Exception handling compile error: Exception; must be caught or declared to be thrown -
I'm not sure why I'm getting this error and I'm happy that something is clear to me. It happens on line: Verify.Validate (number); With error: Program5.java 23: Error: unreported exception exception; Should be thrown to catch or declare. Any suggestions would be appreciated.
- Chris
import java.util.Scanner; Public classroom program 5 {public stationary zero main (string [] args) / Driver class should instantiate a verification object with a range of 10 to 100. Verify = new verification (10, 100); // Prompt the user to input a number within the specific category. System.out.print ("A number input between 10-100:"); // Use a scanner to read user input as an input Scanner input = new scanner (System.in); Int number = input.nextInt (); // Try to try / hold test {Verify.Validate (number); System.out.println ("Number entered:" + number); } Hold (numberless exception before) {System.out.println (ex.getMessage ()); } Hold (NumberLowException pre) {System.out.println (ex.getMessage ()); } Catch (number pre-expired) {System.out.println (ex.getMessage ()); According to your code, there are 3 types of exceptions to your valid (int). ) The method may be thrown: 1) Number high expection
2) Number lava exposition
3) Number exceptions
The valid number (aNT) method may look like this: High zero conviction in number zero, number exception, number exceptions {if ( Number> 100) New number throws hyacension ("number is high"); If the number (10 & amp; nbsp; 10; 10) is inserted the new number is lower option ("number is less"); If (number & lt; 0) new number throw negative exception ("number is negative"); Else System.out.println ("Your entry number is valid"); }
Now, when compiling your code, you get error at:
capture (pre-numbered expression) // line 23, where are you getting the error {system.out.println (ex.getMessage ()); }
and generated error is:
Error: unreported exception exception;
This indicates that the thrown exception is higher (super class) type than specified with catch blocks.
Then, anywhere in your valid () method, you are throwing exceptions to the exception type. Just fix it and you will be fine
Comments
Post a Comment