-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexception.java
More file actions
33 lines (32 loc) · 1.24 KB
/
exception.java
File metadata and controls
33 lines (32 loc) · 1.24 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
import java.util.Scanner;
import java.util.InputMismatchException;
public class exception {
public static void main(String[]args)
{
//exception = event that interrupts normal flow of a program
// (dividing by zero,file not found,mismatch input tpe)
// surround any dangerous code with try{} b;ock
// try{},catch{},finally{}
try(Scanner sc=new Scanner(System.in))
{
System.out.print("Enter a number : ");
int num=sc.nextInt();//on typing wrong i/p we get InputMismatchException
System.out.println(1/0);//gives arithmeticException
}
// catch(ArithmeticException e){//copy these exceptions from the terminal and use a nickname e with it
// System.out.println("you cant divide 1 by 0! IDIOT");
// }
// catch(InputMismatchException e){
// System.out.println("Thats not a number");
// }
catch(Exception e)
{
//safety net { professional way for all exceptions}
System.out.println("Something went wrong");
}
finally
{
System.out.println("this always executes");
}
}
}