473,545 Members | 2,080 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

An Introduction to Exceptions - Ch. 1

Nepomuk
3,112 Recognized Expert Specialist
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, 202 views)
Sep 19 '07 #1
0 3826

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

Similar topics

14
2067
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 install Mono and shows how to compile your first Cross Platform application.
14
2810
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 contents, use the "Bookmarks" tab in Adobe Acrobat. Comments, corrections, praise, nits, etc., are still welcome!
12
3571
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 utilities that allows you to view them with live rotation in a web browser. Also, it includes a introductory tutorial to POV-Ray.
2
4282
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, so if I need to elaborate on something please ask for it. We have a UserControl with a WebBrowser on it. This UserControl is instantiated a few...
5
6264
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 JDK 6 downloadable from http://java.sun.com/javase/downloads/index.jsp. I will be using JDK 5(update 8)
0
3880
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 programming but on Java programming but I will cover all the important aspects of object oriented programming as Java has full support for object-oriented...
0
5321
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 compiler doesn't know, what to do then. You can deal with this in two different ways: Throw the Exception further (pass it on to some other...
0
6489
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 NullPointerException or ArrayIndexOutOfBoundsException. All of these extend the basic class Exception. In general, you can sort Exceptions into two groups:...
0
7470
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7405
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7811
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7428
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5334
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4949
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3455
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1887
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1019
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.