KidsLovePC Contact Us
  Home   PHP  Graphics ASP  JavaScript   J2EE  HTML  JAVA   Swing   Webtip  Forums  Lyric  Free sheet music  Ringtones

dish, directive installerLet us pretend to be policeman. In the last class , we "witness a crime". We get an Exception--java.lang.NumberFormatException. Look at that error message, "thermometer.java:8" tells us line 8 in thermometer.java is causing problem. As a policeman, we should catch this bad guy.

How to analyze it? First, put that line (temperature=Double.parseDouble(args[0]);) into try {}. Just like

try{temperature=Double.parseDouble(args[0]);}

Does try {} look like a handcuff?

Just below try{...}, we need catch(NumberFormatException nfe){...}. Right now, we just print out what that bad guy say by using

catch(NumberFormatException nfe)

{System.out.println("Catch Exception"+nfe.getMessage());

System.exit(9);}

System.exit(9); means stop running program. (Stop crime!, huh?).

The following is the whole code using try and catch.

class thermometer
{
public static void main(String[] args)
{
double temperature=0;
try{
temperature=Double.parseDouble(args[0]);
}
catch(NumberFormatException nfe)
{
System.out.println("Catch Exception="+nfe.getMessage ());
System.exit (9);
}
if(temperature>100.4)
System.out.println ("Beep Beep Beep");
else
System.out.println("No fever.");
}
}

Compile and run: java thermometer abc

You will see:

Catch Exception=For input string: "abc"

This tells us JAVA boy convert abc to double. JAVA boy doesn't know how to do it so he gives you a wrong message to remind you.

 

Try & Catch Exceptions
 
How to install JDK
Homework Solutions
 
JAVA JTable
JAVA JTree
JAVA JComboBox
JAVA JTabbedPane
Java JList
JAVA JToolTip
Javascript
WebTip
Forums
Best JAVA books
Head First JAVA, Second Editon

Effective Java Programming Language Guide

Professional Java, JDK 5 Edition

Ivor Horton's Beginning Java 2, JDK 5 Edition

 

 

 

Best books about JAVA programming from Amazon
Professional Java, JDK 5 Edition (Paperback Simply Java Programming: An Application-Driven Tutorial Approach (Paperback) Effective Java Programming Language Guide (Paperback) Head First Java, 2nd Edition (Paperback) Java : An Introduction to Problem Solving and Programming (4th Edition)

 

Printer Inkjet A Z lyricsLearn ChineseHome|PHP|Swing|HTML|JavaScript|ASP|JAVA|Forum|WebTip|Contact Us
Copyright © kidslovepc.com All rights reserved(new). Privacy|Copyright
designed by info@kidslovepc.com

Please help us cover the web site maintenance fee.
barcode scanner Thermal Printer credit card machine bar code printer