15
Exception Handling in Java Dr. L. Jololian

Exception Handling in Java

Embed Size (px)

DESCRIPTION

Exception Handling in Java. Dr. L. Jololian. vector ProductName; vector ProductPrice; void main(void) { string name; float price; cout > name; cout > price; try { addToInventory(name, price); }. - PowerPoint PPT Presentation

Citation preview

Page 1: Exception Handling in Java

Exception Handlingin Java

Dr. L. Jololian

Page 2: Exception Handling in Java

vector<string> ProductName;vector<float> ProductPrice;

void main(void) { string name;float price;

cout << "Enter name: ";cin >> name;cout << "enter price: ";cin >> price;

try { addToInventory(name, price);}

Page 3: Exception Handling in Java

catch(string n) { cout << n << " has " << n.length() << " characters" << endl; cout << "Error: name is too long" << endl; } catch(float p) { if( p > 100) cout << "price is too high" << endl; if( p < 0) cout << "price may not be negative" << endl; }}

void addToInventory(string nm, float pr) {if( nm.length() > 5) throw nm;if( pr > 100 || pr < 0) throw pr;ProductName.push_back(nm);ProductPrice.push_back(pr);

}

Page 4: Exception Handling in Java

import javax.swing.*;

public class Example1 { public static void main(String[] args) { try {

String str1, str2;int num1, num2, result;

str1 = JOptionPane.showInputDialog(" first integer");str2 = JOptionPane.showInputDialog(“second integer");num1 = Integer.parseInt(str1);num2 = Integer.parseInt(str2);result = num1/num2;JOptionPane.showMessageDialog(null,

"The quotient is " + result, "Division", JOptionPane.PLAIN_MESSAGE);

} catch (ArithmeticException ex) {

Page 5: Exception Handling in Java

} catch (ArithmeticException ex) {JOptionPane.showMessageDialog(null, "Division by 0 has occured","ERROR", JOptionPane.ERROR_MESSAGE);

System.out.println("An error has occured"); } }}

Page 6: Exception Handling in Java
Page 7: Exception Handling in Java
Page 8: Exception Handling in Java

import javax.swing.*;

public class Example2 {

public static void main(String[] args) { try {

int arr[] = { 10, 20, 30 };

for(int i=0; i<4; i++)System.out.println(arr[i]);

} catch (IndexOutOfBoundsException ex) {

Page 9: Exception Handling in Java

} catch (IndexOutOfBoundsException ex) {JOptionPane.showMessageDialog(null,

"index out of bound", "ERROR",JOptionPane.ERROR_MESSAGE);

System.out.println("An error has occured"); } }

}

Page 10: Exception Handling in Java
Page 11: Exception Handling in Java

public class Example3 {

public static void main(String[] args) { try {

String str1, str2;int num1, num2, result;str1 = JOptionPane.showInputDialog("first integer");str2 = JOptionPane.showInputDialog("second integer");num1 = Integer.parseInt(str1);num2 = Integer.parseInt(str2);result = divide(num1, num2);JOptionPane.showMessageDialog(null,

"The quotient is " + result, "Division",JOptionPane.PLAIN_MESSAGE);

} catch (ArithmeticException ex) {

Page 12: Exception Handling in Java

} catch (ArithmeticException ex) {JOptionPane.showMessageDialog(null,

"Div. by 0 has occured", "ERROR",JOptionPane.ERROR_MESSAGE);

System.out.println(“Error has occured"); } } static int divide(int n1, int n2) {

int res = n1/n2;return res;

}

}

Page 13: Exception Handling in Java
Page 14: Exception Handling in Java

public class Example4 {

public static void main(String[] args) { try {

String str1, str2;int num1, num2, result;int arr[] = { 1, 2, 3};

str1 = JOptionPane.showInputDialog("Enter first integer number");

str2 = JOptionPane.showInputDialog("Enter first integer number");

num1 = Integer.parseInt(str1);num2 = Integer.parseInt(str2);result = num1 / num2;

Page 15: Exception Handling in Java

for(int i=0; i<10; i++)System.out.println(arr[i]);

JOptionPane.showMessageDialog(null, "The quotient is " + result, "Division",JOptionPane.PLAIN_MESSAGE);

} catch (ArithmeticException ex) {JOptionPane.showMessageDialog(null,

"Div. by 0 has occured", "ERROR",JOptionPane.ERROR_MESSAGE);

System.out.println("Error has occured"); } catch (IndexOutOfBoundsException ex) {

JOptionPane.showMessageDialog(null, "index out of bound", "ERROR",JOptionPane.ERROR_MESSAGE);

System.out.println("An error has occured"); } }}