-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaExceptions.java
More file actions
46 lines (41 loc) · 1.32 KB
/
JavaExceptions.java
File metadata and controls
46 lines (41 loc) · 1.32 KB
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package oops2;
/**
*
* @author Sunil Shetty
*/
public class JavaExceptions {
public static void main(String[]args){
//int[] mynum={1,2,3};
//System.out.println(mynum[5]);//raise array index out of range error
//using try catch
try
{
int[] num={1,2,3};
System.out.println(num[5]);
}
catch(Exception e){
System.out.println("Some thing went wrong");
}
finally{//optinal it lets you execute code after try catch regardless of the result
System.out.println("try catch is executed");
}
System.out.println("WELCOME");
//throw keyword is used to create a custom error
//program to throw an exception if age is below 18(print access denied) else print access granted
checkAge(17);
}
static void checkAge(int age)
{
if(age<18)
{
throw new ArithmeticException("Access Denied! - you must be atleast 18 years old");
}
else
System.out.println("Access granted");
}
}