peti446
peti446

Reputation: 434

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.

Upvotes: 0

Views: 83

Answers (2)

user1438038
user1438038

Reputation: 6059

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, ParserConfigurationException, 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

Upvotes: 0

Andremoniy
Andremoniy

Reputation: 34900

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...

Upvotes: 1

Related Questions