java.lang.ClassCastException
Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance.
Implemented Interfaces:
Serializable
Declaration
public class ClassCastException extends RuntimeException {@java.io.Serialprivate static final long serialVersionUID =-9223365651070458532L;public ClassCastException() {super();}public ClassCastException(String s) {super(s);}}
Methods
1. ClassCastException()
java.lang.ClassCastException.ClassCastException()
Constructs a ClassCastException with no detailed message.
Approach
Java
public class ClassCastException1 {public static void main(String[] args) {ClassCastException castException =new ClassCastException();System.out.println(castException);}}
Output:
java.lang.ClassCastException
2. ClassCastException(String s)
java.lang.ClassCastException.ClassCastException(String s)
Constructs a ClassCastException with the specified detail message.
Parameters: s the detailed message.
Approach
Java
public class ClassCastException2 {public static void main(String[] args) {String s = "HELLO";ClassCastException castException =new ClassCastException(s);System.out.println(castException);}}
Output:
java.lang.ClassCastException: HELLO
Example
Java
package com.ClassCastException;public class ClassCastExceptionExample {public static void main(String[] args) {Object x = 100;System.out.println((String) x);}}
Output:
Exception in thread "main" java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.String (java.lang.Integer and java.lang.String are in module java.base of loader 'bootstrap') at com.ClassCastException.ClassCastExceptionExample.main(ClassCastExceptionExample.java:6)
Some other Errors/Exceptions
AbstractMethodError: Thrown when an application tries to call an abstract method.
ArithmeticException: Thrown when an exceptional arithmetic condition has occurred.
ArrayIndexOutOfBoundsException: Thrown to indicate that an array has been accessed with an illegal index.
ArrayStoreException: Thrown to indicate that an attempt has been made to store the wrong type of object into an array of objects.
AssertionError: Thrown to indicate that an assertion has failed.
BootstrapMethodError: Thrown to indicate that an invoke dynamic instruction or a dynamic constant failed to resolve its bootstrap method and arguments, or invoke dynamic instruction the bootstrap method has failed to provide a call site with a target of the correct method type, or for a dynamic constant, the bootstrap method has failed to provide a constant value of the required type.
ClassCastException: Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance.
ClassCircularityError: Thrown when the Java Virtual Machine detects a circularity in the superclass hierarchy of a class is loaded.
ClassFormatError: Thrown when the Java Virtual Machine attempts to read a class file and determines that the file is malformed or otherwise cannot be interpreted as a class file.
ClassNotFoundException: Thrown when an application tries to load in a class through its string name using: The forName method in class Class. The findSystemClass method in class ClassLoader. The loadClass method in class ClassLoader.
CloneNotSupportedException: Thrown to indicate that the clone method in class Object has been called to clone an object, but the object's class does not implement the Cloneable interface.
Error: An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch.
Exception: The class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch.
ExceptionInInitializerError: Signals that an unexpected exception has occurred in a static initializer.
IllegalAccessError: Thrown if an application attempts to access or modify a field, or to call a method that it does not have access to.
IllegalAccessException: An IllegalAccessException is thrown when an application tries to reflectively create an instance (other than an array), set or get a field, or invoke a method, but the currently executing method does not have access to the definition of the specified class, field, method or constructor.
IllegalArgumentException: Thrown to indicate that a method has been passed an illegal or inappropriate argument.
IllegalCallerException: Thrown to indicate that a method has been called by an inappropriate caller.
IllegalMonitorStateException: Thrown to indicate that a thread has attempted to wait on an object's monitor or to notify other threads waiting on an object monitor without owning the specified monitor.
IllegalStateException: Signals that a method has been invoked at an illegal or inappropriate time.
No comments:
Post a Comment