Upload
daquan-eaton
View
18
Download
0
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
Exception Handlingin Java
Dr. L. Jololian
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);}
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);
}
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) {
} catch (ArithmeticException ex) {JOptionPane.showMessageDialog(null, "Division by 0 has occured","ERROR", JOptionPane.ERROR_MESSAGE);
System.out.println("An error has occured"); } }}
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) {
} catch (IndexOutOfBoundsException ex) {JOptionPane.showMessageDialog(null,
"index out of bound", "ERROR",JOptionPane.ERROR_MESSAGE);
System.out.println("An error has occured"); } }
}
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) {
} 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;
}
}
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;
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"); } }}