Create an application to check Exception in Roman Numbers also convert it and check its range.
Problem
Statement :
Write a class to represent Roman numerals. The class should have two constructors. One constructs a Roman numeral from a string such as "XVII" or "MCMXCV". It should throw a NumberFormatException if the string is not a legal Roman numeral. The other constructor constructs a Roman numeral from an int. It should throw a NumberFormatException if the int is outside the range 1 to 3999.In addition, the class should have two instance methods. The method toString() returns the string that represents the Roman numeral. The method toInt() returns the value of the Roman numeral as an int.
Introduction :
Java
supports
five Exception handling statements
·
Try
·
Catch
·
Throw
·
Throws
·
Finally
A method catches an
exception using a combination of the try and catch keywords. A try/catch block
is placed around the code that might generate an exception. Code within a
try/catch block is referred to as protected code. The code which is prone to
exceptions is placed in the try block. When an exception occurs, that exception
occurred is handled by catch block associated with it. Every try block should
be immediately followed either by a catch block or finally block. A catch
statement involves declaring the type of exception you are trying to catch. If
an exception occurs in protected code, the catch block (or blocks) that follows
the try is checked. If the type of exception that occurred is listed in a catch
block, the exception is passed to the catch block much as an argument is passed
into a method parameter.
If a method does not
handle a checked exception, the method must declare it using the throws
keyword. The throws keyword appears at the end of a method's signature. You can
throw an exception, either a newly instantiated one or an exception that you
just caught, by using the throw keyword. Try to understand the difference
between throws and throw keywords, throws is used to postpone the handling of a
checked exception and throw is used to invoke an exception explicitly.
The finally block
follows a try block or a catch block. A finally block of code always executes,
irrespective of occurrence of an Exception. Using a finally block allows you to
run any cleanup-type statements that you want to execute, no matter what
happens in the protected code
Program