您的位置:寻梦网首页编程乐园Java天地Core JavaJava Lecture Notes

Introduction

Content

Apply

Reflect

Extend

previous.gif
 (3087 bytes)

next.gif
 (2959 bytes)

 

Content Index

Content Page # 43

Java run-time exceptions

In Java, a ‘run-time exception’ is one that does not have to be caught by the program. The compiler will not report an error if the programmer does not handle a run-time exception. An example of a run-time exception is ArithmeticException . This exception occurs when the program tries to carry out an arithmetic operation which cannot succeed (like dividing by zero). The programmer does not have to handle this exception. 

What happens if the exception is thrown and the programmer does not handle it? The program will automatically be stopped, and the name of the exception displayed. This is a very ugly way for a program to finish, so the programmer may choose to handle the exception anyway.

Handling a run-time exception is no different from handling a normal exception. Here is an example of the use of ArithmeticException :

try

  {

  int x = 2/0;

  }

  catch (ArithmeticException)

    {

    // Display a helpful error message here

    }

Run-time exceptions differ from ordinary exceptions in that they are subclasses of RuntimeException , rather than of Exception .

Back to top

basicline.gif (169 bytes)

RITSEC - Global Campus
Copyright ?1999 RITSEC- Middlesex University. All rights reserved.
webmaster@globalcampus.com.eg