Exercise
Exceptions V2
Objetive
Create a program to ask the user for a real number and display its square root. Errors must be trapped using "try..catch".
Does it behave as you expected?
Example Code
import java.util.*;
public class Main
{
public static void main(String[] args)
{
float result;
float num;
System.out.print("Enter Number ");
try
{
num = Float.parseFloat(new Scanner(System.in).nextLine());
result = (float)Math.sqrt(num);
System.out.printf("The result is: %1$s" + "\r\n", result);
}
catch (RuntimeException e)
{
System.out.println("Error, I cannot calculate the Square Root");
}
}
}