tailieunhanh - Sams Teach Yourself Java 6 in 21 Days 5th phần 4

Ví dụ, lớp parseInt () phương pháp trong lớp Integer có thể được sử dụng với một đối số chuỗi, trả về một đại diện int của chuỗi đó. Tuyên bố sau đây cho thấy làm thế nào parseInt () phương pháp có thể được sử dụng:Trong tuyên bố trước đó, giá trị String "42" là được trả về bởi parseInt () là một số nguyên với một giá trị là 42, | Simpo PDF Merge and Split Unregísteredverson -YHttp yWWW exceptions. It could even throw no exceptions at all. That means that you can have the following two class definitions and things will work just fine public class RadioPlayer public void startPlaying throws SoundException body of method public class StereoPlayer extends RadioPlayer public void startPlaying body of method The converse of this rule is not true A subclass method cannot throw more exceptions either exceptions of different types or more general exception classes than its superclass method. Creating and Throwing Your Own Exceptions There are two sides to every exception the side that throws the exception and the side that catches it. An exception can be tossed around a number of times to a number of methods before it s caught but eventually it will be caught and dealt with. Who does the actual throwing Where do exceptions come from Many exceptions are thrown by the Java runtime or by methods inside the Java classes themselves. You also can throw any of the standard exceptions that the Java class libraries define or you can create and throw your own exceptions. Throwing Exceptions Declaring that your method throws an exception is useful only to your method s users and to the Java compiler which checks to make sure that all your exceptions are being handled. The declaration itself doesn t do anything to actually throw that exception should it occur you must do that yourself as needed in the body of the method. You need to create a new instance of an exception class to throw an exception. After you 7 have that instance use the throw statement to throw it. 198 Simpo P DFeMergeandr Split Unregistered Version - http Here s an example using a hypothetical NotInServiceException class that is a subclass of the Exception class NotInServiceException nise new NotInServiceException throw nise You can throw only objects that implement the Throwable interface. Depending on the .

TỪ KHÓA LIÊN QUAN