You are on page 1of 2

/*PROGRAM FOR USER DEFINED EXCEPTION*/

Exp.html
<html>

<head>

<title>User defined Exception</title>

</head>

<body>

<font size="3">USER DEFINED EXCEPTION</font><br><br>

<form action="Exception.php" method="post">

Enter the Name:<input type="text" name="strg"><br>

Enter the Age:<input type="text" name="num"><br>

<input type="submit" value="submit"></form>

</body>

</html>

Exception.php
?php

$name=$_POST['strg'];

$age=$_POST['num'];

classNameException extends Exception{}

classAgeException extends Exception{}

try

if(preg_match('/[^a-z]/i',$name))

{
throw new NameException();

elseif(preg_match('/[^0-9]/i',$age))

throw new AgeException();

else

echo"The given name is ".$name.'<br>';

echo"The given age is ".$age.'<br>';

echo"It is valid<br>";

catch(NameException $e)

echo"The given name is invalid "."<br>";

echo"$name contains numeric other than a-z A-Z";

catch(AgeException $e)

echo"The given age is invalid "."<br>";

echo"$age contains string other than 0-9";

?>