简体   繁体   中英

Exceptions is valid for all exceptions?

Maybe this is a stupid question, did Exception accept all?, for example IOException, NoSuchAlgorithmException,InvalidKeySpecException... so when i call this method:

public String exception(Exception e){
    StringWriter errors = new StringWriter();
    e.printStackTrace(new PrintWriter(errors));
    return errors.toString();
}

it will accept all exceptions and return them with the original exception name?

Thx for all, and sorry for that stupid question i just started java and want to make sure that point.

The super class for all exceptions and errors is Throwable . So, I don't know how you will use your method, but for error&exception handling in threads, for example, you should use this one:

try { ... } catch (Throwable t) { ... }

UPD . BTW, nobody restricts create own exception from Throwable and throw it instead of Exception descendants:

throw new Throwable() { ... };

So, you method will not process such exceptions...

Yes, your code can handle all e instanceof Exception .

Exception is the base class of all exceptions, there is a list of direct subclasses in the JavaDocs :

AclNotFoundException, ActivationException, AlreadyBoundException, ApplicationException, AWTException, BackingStoreException, BadAttributeValueExpException, BadBinaryOpValueExpException, BadLocationException, BadStringOperationException, BrokenBarrierException, CertificateException, CloneNotSupportedException, DataFormatException, DatatypeConfigurationException, DestroyFailedException, ExecutionException, ExpandVetoException, FontFormatException, GeneralSecurityException, GSSException, IllegalClassFormatException, InterruptedException, IntrospectionException, InvalidApplicationException, InvalidMidiDataException, InvalidPreferencesFormatException, InvalidTargetObjectTypeException, IOException, JAXBException, JMException, KeySelectorException, LastOwnerException, LineUnavailableException, MarshalException, MidiUnavailableException, MimeTypeParseException, MimeTypeParseException, NamingException, NoninvertibleTransformException, NotBoundException, NotOwnerException, ParseException, Parser ConfigurationException, PrinterException, PrintException, PrivilegedActionException, PropertyVetoException, ReflectiveOperationException, RefreshFailedException, RemarshalException, RuntimeException, SAXException, ScriptException, ServerNotActiveException, SOAPException, SQLException, TimeoutException, TooManyListenersException, TransformerException, TransformException, UnmodifiableClassException, UnsupportedAudioFileException, UnsupportedCallbackException, UnsupportedFlavorException, UnsupportedLookAndFeelException, URIReferenceException, URISyntaxException, UserException, XAException, XMLParseException, XMLSignatureException, XMLStreamException, XPathException

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM