Thread

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

package exceptionHandling;

import java.io.FileNotFoundException;
import java.io.IOException;

public class OverrideExceptions {

public static void main(String[] args) {

class Parent{
public void helloEveryone() throws IOException{
System.out.println("Hello parent..");
}
}

class Child extends Parent{


@Override
public void helloEveryone() throws FileNotFoundException {
System.out.println("Hello child...");
}
}
------------------------
java.lang.ArithmeticException: / by zero
Cost per day of the item is 5
thanks teacher
i'm fixing it

try {
//define two numbers
int num1 = 100, num2 = 0;
int result = num1 / num2; // divide by zero
//print the result
System.out.println("Result = " + result);
}
catch (ArithmeticException e) {
System.out.println("ArithmeticException:Division by Zero");
}
}
-------------
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter two integer numbers");

// Read two integer numbers.


int num1 = sc.nextInt();
int num2 = sc.nextInt();
System.out.println(num1 + "/" + num2 + " = " + (num1/num2));
}
}

-------------------------
public static void main(String[] args) {
ArrayExample arrExample = new ArrayExample();
Integer[] arr = arrExample.createRandom();

Scanner scaner = new Scanner(System.in);


System.out.println("\nVui lòng nhập chỉ số của một phần tử bất kỳ: ");
int x = scaner.nextInt();
try {
System.out.println("Giá trị của phần tử có chỉ số 5" + x + " là " +
arr[x]);
} catch (IndexOutOfBoundsException e) {
System.err.println("Chỉ số vượt quá giới hạn của mảng");
}
}

// Importing generic Classes/Files


import java.util.*;

public class GFG {

// Main driver method


public static void main(String args[])
throws ArrayIndexOutOfBoundsException
{

// Taking input from user


Scanner s = new Scanner(System.in);

// Storing user input elements in an array


int arr[] = new int[5];

// Try block to check exception


try {
// Forcefully iteration loop no of times
// these no of times > array size
for (int i = 0; i < 6; i++) {

// Storing elements through nextInt()


arr[i] = s.nextInt();
}
}
catch (ArrayIndexOutOfBoundsException e) {
// Print message when any exception occurs
System.out.println(
"Array Bounds Exceeded...\nTry Again");
}
}
}

You might also like