[go: up one dir, main page]

0% found this document useful (0 votes)
10 views3 pages

Week 7 A

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Week 7 A

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

a) Read two integers as strings Num1 and Num2 to perform division.

The program throw a


Number Format Exception if Num1 or Num2 cannot be converted to integers and If Num2
is Zero throw an Arithmetic Exception. Display the exception message.

import java.util.*;

public class ExcepDemo1{

public static void main (String args[]) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the number num1");

String s1 = sc.next();

System.out.println("Enter the number2");

String s2=sc.next();

try

int num1 = Integer.parseInt(s1);

int num2 = Integer.parseInt(s2);

System.out.println("num1 is " +num1);

System.out.println("num2 is " +num2);

if(num2 ==0)

throw new ArithmeticException ("Division Error");

int res = num1/num2;

System.out.println("The result is " +res);

catch(NumberFormatException e) {

System.out.println("The numbers must be numeric data");

System.out.println("Exception " +e);


}

catch(ArithmeticException e){

System.out.println("num2 must not be zero");

System.out.println("Exception" +e);

finally {

System.out.println("Finally block is executed");

System.out.println("Remaining statements");

You might also like