1 / 15

Exception Handling in Java

Exception Handling in 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); }.

Download Presentation

Exception Handling in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Exception Handlingin Java Dr. L. Jololian

  2. 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); }

  3. 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); }

  4. 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) {

  5. } catch (ArithmeticException ex) { JOptionPane.showMessageDialog(null, "Division by 0 has occured", "ERROR", JOptionPane.ERROR_MESSAGE); System.out.println("An error has occured"); } } }

  6. 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) {

  7. } catch (IndexOutOfBoundsException ex) { JOptionPane.showMessageDialog(null, "index out of bound", "ERROR", JOptionPane.ERROR_MESSAGE); System.out.println("An error has occured"); } } }

  8. 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) {

  9. } 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; } }

  10. 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;

  11. 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"); } } }

More Related