|
To access the contents, click the chapter and section titles.
Sams Teach Yourself Visual J++ 6 in 21 Days
Declaring ExceptionsJava requires that methods either catch or declare all non-runtime exceptions that can be thrown within the methods scope. This means that if a method chooses not to catch an exception, it must declare that it can throw it. Sometimes, its not a good idea to catch exceptions. For example, if you catch an exception deep down in the call stack, you might not know how or why your method is being called. What would you do with the exception after it was caught? Because you dont know enough about what caused your method to be called, you might not be able to adequately handle the exception. In that case, it would be better to pass the exception back down the call stack; you should declare the exception rather than catch it. To declare an exception, simply add the throw statement to your method signature, followed by the exception name. To declare more than one exception, separate each name by a comma. Suppose, for example, that you wanted to define a new method that calls the myDivide() method created earlier. Instead of implementing an exception handler for myDivide() in your new method, you can declare the potential exceptions to pass them down the call stack: public void myMath() throws ArithmeticException, MyOwnException { // Do stuff here } Some programmers realize that it takes a lot less time to declare an exception than it does to write an appropriate exception handler, and they are often tempted to simply pass the buck rather than handle the exception. As a general rule, this is a bad idea. However, there are times when it is appropriate to declare an exception rather than handle it. SummaryTo use variables in programming, they must first be declared and initialized. If they arent initialized, Java will do it for you. However, good programming style doesnt rely on auto-initialization. Variable declaration consists of two parts: a data type and an identifier. Variable types are either primitives or references to objects. Once declared, a value is introduced to a scope, which can then be stored in a variable, provided that it is within the limits of the variables type. Furthermore, expressions can be utilized to perform calculations and can also be used to store values. Expressions make use of operators, which are the symbols (or group of symbols) a program uses to indicate a specific operation to perform (such as addition, multiplication, or division). Operators are evaluated in order of precedence, and as either left-to-right or right-to-left, depending on their associativity. Control-flow statements, such as branching statements and loops, direct Javas program execution. In addition, exception handlers are used to control the flow of program execution when an exception is thrown. Javas exception mechanism is a general-purpose error-processing mechanism, which detects errors in code by throwing objects that disrupt the normal flow of program execution. Q&AQ How does a program store values? A Programs store most values in variables. Not only do variables store values, but they can be altered as well. Variables can control the flow of a programs execution and affect the overall state of a program. Q How do you declare variables? A Variables must be declared before theyre used. Variable declarations have two parts: the data type and the identifier. The data type determines the kind of data and the range of values a variable has. Examples of this are int, long, and double. The identifier is the name that the programmer gives a variable. Q What does variable initialization mean? A When variables are declared, they can be initialized with a starting value. Here are some examples of variable initialization: int nWidth = 500; int nHeight = 200; boolean bQuit = false; Q How is an array initialized? A An array is initialized in two steps. First, you declare the number of elements the array will have. Then each element in the array is individually initialized. Heres an example: int nValues[] = new int[25]; for( int i=0; i<25; i++) nValues[i] = i * 2; Q What is meant by variable scope? A Every variable has an associated scope. The scope is the extent to which the variable can be used. A variables scope begins where its declared, and ends with the closing brace (}) of the block of code its in. You can access a variable only within its scope. Any attempt to access it outside of its scope generates a compiler error. Q What are expressions? A Expressions are statements that, when executed, result in a value. When programming, you use expressions all the time, sometimes without realizing it. Q When you use a for loop, is there any way to get out of it before the count has finished? A Yes, you can use the break statement. The break statement exits the current for loop and executes the next statement immediately after the end of the for loop. Q Is it optional to catch exceptions? A No. If a method throws an exception, your code must catch it. Otherwise, a compile error will occur.
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |