Title: Exception Handling in Java
1Exception Handlingin Java
2vectorltstringgt ProductName vectorltfloatgt
ProductPrice void main(void) string
name float price cout ltlt "Enter name
" cin gtgt name cout ltlt "enter price " cin
gtgt price try addToInventory(name,
price)
3catch(string n) cout ltlt n ltlt " has " ltlt
n.length() ltlt " characters" ltlt endl cout
ltlt "Error name is too long" ltlt endl
catch(float p) if( p gt 100) cout ltlt
"price is too high" ltlt endl if( p lt 0)
cout ltlt "price may not be negative" ltlt endl
void addToInventory(string nm, float pr)
if( nm.length() gt 5) throw nm if( pr gt 100
pr lt 0) throw pr ProductName.push_back(nm)
ProductPrice.push_back(pr)
4import 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.showInputDial
og(second integer") num1 Integer.parseInt(str
1) 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(No Transcript)
7(No Transcript)
8import javax.swing. public class Example2
public static void main(String args)
try int arr 10, 20, 30
for(int i0 ilt4 i) System.out.println(arri
) catch (IndexOutOfBoundsException
ex)
9 catch (IndexOutOfBoundsException ex)
JOptionPane.showMessageDialog(null, "index
out of bound", "ERROR", JOptionPane.ERROR_MESSAG
E) System.out.println("An error has
occured")
10(No Transcript)
11public class Example3 public static void
main(String args) try String str1,
str2 int num1, num2, result str1
JOptionPane.showInputDialog("first
integer") str2 JOptionPane.showInputDialog("se
cond integer") num1 Integer.parseInt(str1) n
um2 Integer.parseInt(str2) result
divide(num1, num2) JOptionPane.showMessageDialog
(null, "The quotient is " result,
"Division", JOptionPane.PLAIN_MESSAGE)
catch (ArithmeticException ex)
12 catch (ArithmeticException ex)
JOptionPane.showMessageDialog(null, "Div.
by 0 has occured", "ERROR", JOptionPane.ERROR_M
ESSAGE) System.out.println(Error
has occured") static
int divide(int n1, int n2) int res
n1/n2 return res
13(No Transcript)
14public class Example4 public static void
main(String args) try String str1,
str2 int num1, num2, result int arr 1,
2, 3 str1 JOptionPane.showInputDialog( "E
nter first integer number") str2
JOptionPane.showInputDialog( "Enter first
integer number") num1 Integer.parseInt(str1)
num2 Integer.parseInt(str2) result num1 /
num2
15 for(int i0 ilt10 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_ME
SSAGE) System.out.println("Error
has occured") catch (IndexOutOfBoundsEx
ception ex) JOptionPane.showMessageDialog(null,
"index out of bound", "ERROR", JOptionPane.E
RROR_MESSAGE) System.out.println("An
error has occured")