Exception handling is an event that is running normal process flow in the progra
ID: 3781871 • Letter: E
Question
Exception handling is an event that is running normal process flow in the program, These events usually be some form of error, this is caused by the program ends abnormal HAVE you create a program and when the input does not match the expectations it appears an error weird? exception handling can prevent such a strange error Now I ask you whether the exception handling can be validating such email, input which should contain only digits, or input which should contain only letters if you can, please make a simple program like checking email name check or checking ageExplanation / Answer
Code:-
import java.util.*;
import java.lang.*;
import java.io.*;
class InvalidInputException extends Exception
{
public InvalidInputException(String str)
{
super(str);
}
}
class Email
{
private String name;
public void set(String s) throws InvalidInputException
{
if(s.contains("[a-zA-Z]+")==false)
{
name=s;
}
else
{
InvalidInputException e=new InvalidInputException("String should contain only numbers");
throw e;}
}
}
class UseEmail
{
public static void main(String args[])
{
Email e1=new Email();
try
{
e1.set("Hello");
}
catch(InvalidInputException e)
{
System.out.println(e.getMessage());
}
}
}
Output:
Successfully Executed
String should contain only numbers.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.