Total members 11786 |It is currently Sat Jul 20, 2019 11:24 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





What are exceptions?
-----------------------------------------------------

An exception is Java's method of handling errors that occur while running the program.

This relatively simple chunk of code looks for a file named readme.txt, and opens it without reading it or doing anything once it is opened. The important thing is that if readme.txt doesn't exist, calling the constructor method FileInputStream() will send an exception named FileNotFoundException, strangely enough.
So how do we deal with pesky exceptions? One way to prevent exceptions from prematurely ending a program is to put them in a try..catch block:

java code
try {
FileInputStream fileopener = new FileInputStream("readme.txt");
}
catch(FileNotFoundException ex) {
System.out.println("An exception has occured:\n" + ex.getMessage());
}


What the try..catch block does is attempt to do whatever is in the try block--in this case, opening a file. If the file doesn't exist, it will throw the FileNotFoundException. This time, control will move to the catch block, where the exception has been "caught," and Java will do whatever in the block--in this case, print the words "An exception has occurred:", followed by the message embedded in the exception. (As you might have noticed, exceptions, like many things in Java, are objects, and as such have their own methods.)

Analyzing exceptions:

There are two ways to deal with exceptions:

  • Handle the exception right where it occurs
  • Throw the exception on to the next method

We have already looked at handling the exception immediately by using try..catch statements. You can use catch to clean up unfinished business, explain what kind of error occurred--whatever you'd like, as long as it doesn't depend on anything succeeding in the try block. If you choose to throw the exception, you simply pass it on. Exceptions start in the method where they originated and "bubble up." For example:

java code
public static void parent() {
child();
}
public static void child throws FileNotFoundException() {
grandchild();
}
public static void grandchild throws FileNotFoundException() {
FileInputStream fileopener = new FileInputStream("readme.txt");
}


If it turns out the file readme.txt doesn't exist, FileInputStream() will send an exception to grandchild(), who will send it to child(), who will send it to parent(). At some point, of course, the exception has to be caught; where this is is up to you. To have methods throw exceptions, add the words throws (name of exception) at the end of the line declaring the method. Any exception that isn't in a try..catch block will be bubbled up.



_________________
Please recommend my post if you found it helpful


Author:
Beginner
User avatar Posts: 109
Have thanks: 5 time

updated.


Author:
Mastermind
User avatar Posts: 2707
Have thanks: 74 time
Post new topic Reply to topic  [ 2 posts ] 

  Related Posts  to : What are exceptions and how to handle them!!!
 How to throw Exceptions in Java?     -  
 Creating your own Exceptions in Java     -  
 Handle Cookies using JQuery     -  
 Handle IsPostBack Condition     -  
 How can a GUI component handle its own events     -  
 Handle Focus events with FocusListener     -  
 handle integer overflows and underflows     -  
 Handle Key event-keyboard-Get typed Character and its code     -  
 exceptions catch by a catch clause     -  



Topic Tags

Java Exceptions
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com