|Access||Adobe photoshop||Algoritmi||Autocad||Baze de date||C||C sharp|
|Calculatoare||Corel draw||Dot net||Excel||Fox pro||Frontpage||Hardware|
|Php||Power point||Retele calculatoare||Sql||Tutorials||Webdesign||Windows|
|Asp||Autocad||C||Dot net||Excel||Fox pro||Html||Java|
Youíre not stuck using the Java exceptions. This is important because youíll often need to create your own exceptions to denote a special error that your library is capable of creating, but which was not foreseen when the Java hierarchy was created.
To create your own exception class, youíre forced to inherit from an existing type of exception, preferably one that is close in meaning to your new exception. Inheriting an exception is quite simple:
The inheritance occurs in the creation of the new class:
The key phrase here is extends Exception, which says ďitís everything an Exception is and more.Ē The added code is small Ė the addition of two constructors that define the way MyException is created. Remember that the compiler automatically calls the base-class default constructor if you donít explicitly call a base-class constructor, as in the MyException( ) default constructor. In the second constructor, the base-class constructor with a String argument is explicitly invoked by using the super keyword.
The output of the program is:
You can see the absence of the detail message in the MyException thrown from f( ).
The process of creating your own exceptions can be taken further. You can add extra constructors and members:
A data member i has been added, along with a method that reads that value and an additional constructor that sets it. The output is:
Since an exception is just another kind of object, you can continue this process of embellishing the power of your exception classes. Keep in mind, however, that all this dressing up might be lost on the client programmers using your packages, since they might simply look for the exception to be thrown and nothing more. (Thatís the way most of the Java library exceptions are used.) If this is the case, itís possible to create a new exception type with almost no code at all:
This relies on the compiler to create the default constructor (which automatically calls the base-class default constructor). Of course, in this case you donít get a SimpleException(String) constructor, but in practice that isnít used much.
Politica de confidentialitate|
Adauga cod HTML in site