-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path11.Throwexample.java
50 lines (49 loc) · 1.08 KB
/
11.Throwexample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*Question -
User define exception , try catch blocks , Throw example in Java*/
//Code-
import java.util.Scanner;
class UserDefinedException extends Exception{
String ms;
UserDefinedException()
{
}
UserDefinedException(String ms)
{
this.ms = ms;
}
@Override
public String toString()
{
return "Invalid! UserDefinedException :" + ms;
}
}
public class throwexample {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number :");
try
{
int num = sc.nextInt();
try
{
if(num < 0)
{
throw new UserDefinedException("Number is negative");
}
else if(num >= 0 && num <= 25)
{
throw new UserDefinedException("Number is in the range of 0-25");
}
}
catch (UserDefinedException e){System.out.println(e);}
finally
{
System.out.println("The number entered by the user is: " + num); //this code is always executed
}
}
catch (Exception e)
{
System.out.println(e);
}
}
}