GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — java-programs/Exceptions/exception_2.java at main · IntScription/java-programs

Expand file tree

/

exception_2.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
63 lines (58 loc) · 1.68 KB
/

exception_2.java

Copy path

File metadata and controls

63 lines (58 loc) · 1.68 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
Write a Java program to read n number of integers into an array.
Raise an appropriate Exception (ArithmeticException, InputMismatchException,
ArrayOutOfBoundsException) while performing following operations:
a) Dividing each element by the smallest element in an array.
b) Reading elements from the keyboard
c) Accessing the element from the index specified by the keyboard entry.
Concept of exception handling using multiple catch blocks to be used in this scenario.
*/
package Exceptions;
import java.util.*;
class MultiExcept {
private int arr[];
public void read() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of elements");
int n = sc.nextInt();
arr = new int[n];
System.out.println("Enter "+ n +" elements");
for(int i = 0;i < n; i++) {
arr[i] = sc.nextInt();
}
}
public void divide() {
int small = arr[0];
for(int num:arr) {
if(num < small) {
small = num;
}
}
for(int num:arr) {
System.out.println(num + "/" + small + "=" + num / small);
}
}
public void getElement() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the index to be accessed");
int idx = sc.nextInt();
System.out.println("Element at "+ idx +" is "+ arr[idx]);
sc.close();
}
}
public class exception_2{
public static void main(String[] args) {
MultiExcept ob1 = new MultiExcept();
try {
ob1.read();
ob1.divide();
ob1.getElement();
}catch(InputMismatchException e) {
System.out.println("Illegal Input");
}catch(ArithmeticException e) {
System.out.println("Smallest element is 0");
}catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Illegal Index");
}
}
}
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.