473,385 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,385 developers and data experts.

An Introduction to Exceptions - Ch. 1

Nepomuk
3,112 Expert 2GB
Chapter 1: What is an Exception?
Imagine, you want to write a program for calculating. You start, it works fine, you can add, subtract, multiply and divide. Then there's a question: What about the division by zero?
Say, your division method looks like this:
Expand|Select|Wrap|Line Numbers
  1. public double div(double a, double b)
  2.     {
  3.         return a/b;
  4.     }
  5.  
You try:
Expand|Select|Wrap|Line Numbers
  1. System.out.println("2.0 / 3.0 = " + div(2.0,3.0));
  2. // 2.0 / 3.0 = 0.6666666666666666
  3. System.out.println("6.0 / 2.0 = " + div(6.0,2.0));
  4. // 6.0 / 2.0 = 3.0
  5. System.out.println("1.0 / 0.0 = " + div(1.0,0.0));
  6. // 1.0 / 0.0 = Infinity
  7.  
The answer you get is "Infinity". This answer isn't very pleasing, you want it to give an error.
However, just doing something like this
Expand|Select|Wrap|Line Numbers
  1. double d;
  2. if(div(1.,0.) == Infinity) d = 1;
  3. else d = div(1.,0.);
  4. System.out.println("1.0 / 0.0 = " + c);
  5.  
or this
Expand|Select|Wrap|Line Numbers
  1. double c = (div(1.,0.) == Infinity) ? 1. : div(1.,0.);
  2. System.out.println("1.0 / 0.0 = " + c);
  3.  
won't work - the compiler will tell you that Infinity cannot be resolved. So, what do you do?

In many programming languages, such as Java, the creators of the language thought about this sort of problem and found a solution: Exceptions.
A function, which should give back a value can always throw an exception instead. The function is then terminated, without having fulfilled it's actual task.

In this case, it would look something like this:
Expand|Select|Wrap|Line Numbers
  1. public double div(double a, double b) throws Exception
  2.     {
  3.         if (b==0) throw new Exception();
  4.         return a/b;
  5.     }
  6.  
Now, what have we done?
  1. We have added "throws Exception" to the methods head. This just tells the compiler, that this method might throw an Exception.
  2. We have added a new line to the program:
    Expand|Select|Wrap|Line Numbers
    1. if (b==0) throw new Exception();
    2.  
    This checks, if b is zero and if so it throws a new Exception. Then the function stops and doesn't return any value.
If you compile your program now, it will tell you that there is an Unhandled exception type Exception. This will be covered in the next chapter.

Continue to chapter 2
Attached Files
File Type: zip Chapter1.zip (823 Bytes, 201 views)
Sep 19 '07 #1
0 3807

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: Cletis Tout | last post by:
http://www.codeproject.com/cpnet/introtomono1.asp Introduction to Mono - Your first Mono app By Brian Delahunty The first in a series of articles about Mono. This article explains how to...
14
by: Alf P. Steinbach | last post by:
Not yet perfect, but: http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01_examples.zip To access the table of...
12
by: Xah Lee | last post by:
Of Interest: Introduction to 3D Graphics Programing http://xahlee.org/3d/index.html Currently, this introduction introduces you to the graphics format of Mathematica, and two Java Applet...
2
by: Jeroen | last post by:
We are experiencing a tuff-to-debug problem ever since we introduced a WebBrowser control into our failry large application. I'm not sure if the problem description below contains enough details,...
5
by: r035198x | last post by:
Setting up. Getting started To get started with java, one must download and install a version of Sun's JDK (Java Development Kit). The newest release at the time of writting this article is...
0
by: r035198x | last post by:
Inheritance We have already covered one important concept of object-oriented programming, namely encapsulation, in the previous article. These articles are not articles on object oriented...
0
RedSon
by: RedSon | last post by:
Chapter 2: How to handle Exceptions When you call a program, sometimes you get a message about an Unhandled exception type. This means, that something throws (or might throw) an Exception, but the...
0
RedSon
by: RedSon | last post by:
Chapter 3: What are the most common Exceptions and what do they mean? As we saw in the last chapter, there isn't only the standard Exception, but you also get special exceptions like...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.