How I may help
LinkedIn Profile Email me!
Call me using Skype client on your machine

Reload this page Programming Exception Handling

This page examines the differences in how programming languages detect and intercept events that disrupt the normal flow of a program's execution.

 

Topics this page:

  • Language Differences
  • Java Exception Handling
  • Java Exception Classes
  • Your comments???
  •  

    Site Map List all pages on this site 
    About this site About this site 
    Go to first topic Go to Bottom of this page

    Search

    Set screen Language Differences

      Using exceptions in C# does not in general adversely affect performance, unlike C++.

      VB error handling is limited to On Error GoTo statements.


    Go to Top of this page.
    Previous topic this page
    Next topic this page

    Set screen Java Exception Handling

       

        public void doDaIO() throws IOException {
           // set boolean flag if process is successful.
           ...  
           if( !success ) {
              throw new EOFException("File error 1234");
           }
        }
        
       
      Exceptions result in unconditional but recoverable transfer of control (not a return) from the try block.

      To issue exceptions the code cannot intercept on its own (such as data in the wrong format). It must first create an instance of java.lang.Throwable, then declare the signature of a method with a throws clause with throw objects.

      Subclasses can declare subclasses of the class declared. For example, EOFException (End of File Exception) is a subclass of IOException.

      Among the 50+ subclasses generated by the Java runtime system's Java Exception Classeson this page are:

      • ArithmeticException from divide by zero
      • FileNotFoundException from a file Open failure
      • IOException from a Bad Network Connection
      • ArrayIndexOutOfBoundsException

      Java exceptions class Java developers write throw objects under the Exception subclass derived from the java.lang.Thowable interface. This Throwable class is the superclass of all errors and exceptions in the Java language. Only objects that are instances of this class (or one of its subclasses) are (can be) thrown by the throw statement. Similarly, only this class or one of its subclasses can be the argument type in a catch clause.

      java.lang.Error subclasses (such as the VirtualMachineError subclass thrown by the JVM) are not recoverable. Errors arise when Events external to a program code occur, such as the JVM running out of memory, cause program code to stop.

      Checked?

      An exception is "checked" if the compiler checks whether coding exists to anticipate/handle that exception.

      All classes are checked by the compiler except for java.lang.RuntimeException and Error subclasses, which are unchecked by the compiler.

      The Error subclass of the Throwable class is a sibling class of the Exception subclass. In other words, the compiler does not require coding to declare or catch Exception Errors.

       
      try {

        // The code to protect:
        Inventory.StockRoom srRet =
        srm.getStockRoom((java.lang.String));

      } catch( ThrowableObjectType paramName) {

        // to see the message associated with the Exception:
        e.getMessage();
        // to determine the origin of the Exception:
        e.printStackTrace();
        // to show the Exception name and message:
        e.toString();

      } catch( Inventory.NotAStockItem e) {

        // Handle "Not A Stocked Item" exception ...
        System.out.println("usage Advert input");
        System.exit(1);

      } finally {
         // Clean-up code (close files, etc.)
         // regardless of whether or not an exception occured:
         System.out.println( "Out of catch block");
      }
       
      To detect recoverable exceptions, methods are called from within a try block. Each catch blocks determines whether a type of exception have been thrown, then responds to them by passing off to exception handler code.

      For example, when trying the Thread.wait() method, the code should include a catch for InterruptException.

      Reminder The JRE terminates with a NullPointerException if it can't find an appropriate catch somewhere up the call stack.

      A variable referenced in try or catch blocks must be declared before entering the try block.

      webpage article Dick Baldwin has a Tutorial on Exception Handling

      webpage article David R. Nadeau's Java Exceptions Tutorial

      Exception handling in German is Ausnahmebehandlung.


    Go to Top of this page.
    Previous topic this page
    Next topic this page

    Set screen Java Exception Classes

      The java.lang package defines several classes and exceptions are subclasses of the RuntimeException class for use in exception handlinganother page on this site Some of these classes are not checked to see if they are included in a method's Throws List of exceptions (they do not have to be declared), while some other classes are checked.

      Exception Description Checked
      java.lang.NoClassDefFoundError the default package cannot be found in the folder list within PATH or CLASSPATH environment variable. --
      ArithmeticException Arithmetic errors such as a divide by zero NO
      ArrayIndexOutOfBoundsException Arrays index is not within array.length NO
      ArrayStoreException Assignment of incompatible datatype NO
      ClassNotFoundException Class is not found YES
      CloneNotSupportedException Cloning an object that does not implement Cloneable YES
      IllegalAccessException No access to class YES
      IllegalArgumentException Illegal argument when calling a method NO
      IllegalStateException Illegal state for application or environment NO
      IllegalThreadStateException Operation not compatible with current thread NO
      InstantiationException Instantiating an abstract class YES
      InterruptedException One thread has been interrupted by another thread YES
      NegativeArraySizeException Creating array of negative size NO
      NoSuchFieldException Nonexistent field YES
      NoSuchMethodException Nonexistent method YES
      NullPointerException Invalid use of null reference NO
      NumberFormatException Invalid string for conversion to number NO
      SecurityException Security violation NO
      StringIndexOutOfBoundsException Index for string is out of bounds NO


    Go to Top of this page.
    Previous topic this page
    Next topic this page

    Portions ©Copyright 1996-2010 Wilson Mar. All rights reserved. | Privacy Policy |

    Related:

  • Programming Languages
  • Arrays
  • Applications Development
  • On the web

  • How I may help

    Send a message with your email client program


    Your rating of this page:
    Low High




    Your first name:

    Your family name:

    Your location (city, country):

    Your Email address: 



      Top of Page Go to top of page

    Thank you!