Skip to main content

Posts

Showing posts with the label exceptions

Understanding the Java Exception heirarchy

In the previous section, we learned how to use custom exceptions. We also created a custom exception by subclassing java.lang.Exception. Let us now understand the Java exception hierarchy in greater detail. Play the audio to listen to an explanation of the java Exception class hierarchy. Understanding java.lang.error The video below shows how the JVM can throw an Error for an irrecoverable problem. Click on the 'Start' button to start playing the movie. Understanding RuntimeException The video below explains runtime exceptions. Click on the 'Start' button to start playing the movie.

Custom Exceptions

Till now we have seen why we need Exceptions and how to manage code that could throw Exceptions. We still have not learned how to throw our own Exceptions. Let us first understand the code below. 01 public class DocToPdfConverter { 02    public void convert ( String src, String des ) { 03      Document doc = readDoc ( src ) ; 04      writePdf ( doc, des ) ; 05    } 06 07    private Document readDoc ( String srcPath ) { 08    //read the source file and verify format 09    //if format is incorrect then throw an Exception 10    } 11 12    private void writePdf ( Document doc, String des ) { 13      //convert the document into pdf 14    } 15 } Java2html 01 public class DocToPdfConverter { 02    public void convert ( String src, String des ) { 03      Document doc = nul...

Managing exceptions

When we call a method that can throw an Exception, handling the Exception is not the only option we have. We can also propogate the exception. The audio and code shown below explains this concept. import java.io.*; public class AnExceptionalDilemna {    public static void main ( String args []) {      AnExceptionalDilemna aed = new AnExceptionalDilemna () ;      System.out.println ( "Hello, this is a test program to      understand Exceptions" ) ;      try {        System.out.print ( "Please enter your name: " ) ;        String name = aed.getNameThroughMiddleMan () ;        System.out.println ( "Hey " +name+ " how are you doing?" ) ;      } catch ( IOException ioe ) {        System.out.printl...

How do exceptions work?

Now let us understand how Exceptions work. The code shown below is a simple program that shows how to use Exceptions. Listen to the audio below as you view the code. import  java.io.*; public class  LearningExceptions  {    public static  void  main ( String args []) {      LearningExceptions le =  new  LearningExceptions () ;      System.out.println ( "Hello, this is a test program to        understand Exceptions" ) ;      try  {        System.out.print ( "Please enter your name: " ) ;        String name = le.getName () ;        System.out.println ( "Hey "  + nam...

Why does Java support exception handling?

Here's a nanocast on why we should we use use exceptions to handle error conditions in programs. I hope you enjoy it :-) This is the first episode in a multi-part series on Exception handling in Java. ----- COMMENT: AUTHOR: Azagu EMAIL: DATE: 01/23/2007 10:24:36 AM Hi There, Greetings and Wish you a Happy New Year. I am following your website for the past couple of months and I liked it very much. Are you planning to put the mp3 version of this series as well on the website? It will help the users to load it to their mp3 player and listen to them. You are doing a great job. ----- COMMENT: AUTHOR: Parag DATE: 01/24/2007 03:26:09 PM Hi Azagu, Thank you for your kind words and a very happy new year to you too. Most of the mp3's heavily depend on some code or an example also posted as part of the blog post. I am not sure if the mp3 will be very useful independantly. Also, once I make them downloadable, my hosting bandwidth requirements will increase significan...