A catch-block in a try statement needs to catch exactly the exception that the code inside the try {}
-block can throw (or a super class of that).
try { //do something that throws ExceptionA, e.g. throw new ExceptionA("I am Exception Alpha!"); } catch(ExceptionA e) { //do something to handle the exception, e.g. System.out.println("Message: " + e.getMessage()); }
What you are trying to do is this:
try { throw new ExceptionB("I am Exception Bravo!"); } catch(ExceptionA e) { System.out.println("Message: " + e.getMessage()); }
This will lead to an compiler error, because your java knows that you are trying to catch an exception that will NEVER EVER EVER occur. Thus you would get: exception ExceptionA is never thrown in body of corresponding try statement
.
Related Posts:
- What is a StackOverflowError?
- What is the proper way to handle a NumberFormatException when it is expected?
- Exception in thread “main” java.util.NoSuchElementException
- JavaFX – Exception in Application start method?
- JavaFX – Exception in Application start method? [duplicate]
- Given final block not properly padded
- What is an AssertionError? In which case should I throw it from my own code?
- EOFException – how to handle?
- Can I catch multiple Java exceptions in the same catch clause?
- Exception in thread “main” java.util.NoSuchElementException: No line found
- “NoClassDefFoundError: Could not initialize class” error
- unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
- Exception in thread “main” java.lang.UnsupportedClassVersionError: a (Unsupported major.minor version 51.0)
- “NoClassDefFoundError: Could not initialize class” error
- “NoClassDefFoundError: Could not initialize class” error
- Why do I get an UnsupportedOperationException when trying to remove an element from a List?
- Why do I get “Exception; must be caught or declared to be thrown” when I try to compile my Java code?
- What is difference between Errors and Exceptions?
- Causes of getting a java.lang.VerifyError
- How to create a custom exception type in Java?
- Exception in thread “main” java.lang.ArithmeticException: / by zero
- Java can’t find file when running through Eclipse
- FXML Load exception
- StringIndexOutOfBoundsException String index out of range: 0
- java.lang.ClassNotFoundException:com.mysql.jdbc.Driver [duplicate]
- Connection Java – MySQL : Public Key Retrieval is not allowed
- Why is a ConcurrentModificationException thrown and how to debug it
- Throwing multiple exceptions in Java
- java.lang.ArrayIndexOutOfBoundsException: 0
- Official reasons for “Software caused connection abort: socket write error”
- How can I read a text file in Android?
- How can I catch all the exceptions that will be thrown through reading and writing a file?
- How do you create a dictionary in Java? [closed]
- How do I declare and initialize an array in Java?
- Convert list to array in Java [duplicate]
- Convert String to double in Java
- Initialization of an ArrayList in one line
- Is there a Heap in java?
- What’s the difference between map() and flatMap() methods in Java 8?
- Why do we need copy constructor and when should we use copy constructor in java
- What are the -Xms and -Xmx parameters when starting JVM?
- How to match “any character” in regular expression?
- Running JAR file on Windows 10
- Iterate through a HashMap [duplicate]
- Non-static variable cannot be referenced from a static context
- JAVA invalid maximum heap size. The specified size exceeds the maximum representable size
- Usage of @see in JavaDoc?
- Hadoop “Unable to load native-hadoop library for your platform” warning
- Unreported exception java.lang.Exception; must be caught or declared to be thrown
- How to clear the console?
- How to capitalize the first letter of a String in Java?
- Scanner vs. BufferedReader
- Check jdk/JRE is installed and get path for jvm.dll
- What is Java String interning?
- Rename a file using Java
- What is “String args[]”? parameter in main method Java
- “A java exception has occurred” when opening .jar
- How do I set environment variables from Java?
- Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 0 error in Java
- What’s the advantage of load() vs get() in Hibernate?
- warning: [options] bootstrap class path not set in conjunction with -source 1.5
- “Actual or formal argument lists differs in length”
- TreeMap sort by value
- Encrypt Password in Configuration Files?
- Could not find method compile() for arguments Gradle
- How can I get the current stack trace in Java?
- java.lang.IllegalMonitorStateException: object not locked by thread before wait()?
- WebServlet cannot be resolved to a type
- How do I print out the value of this boolean? (Java)
- How to add an object to an ArrayList in Java
- Getting warning “NDK is missing a ‘platforms” directory.’ with no NDK
- No appenders could be found for logger(log4j)?
- ArrayList<> cannot be resolved to a type
- Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: 0
- Error: Main method not found in class Calculate, please define the main method as: public static void main(String[] args) [duplicate]
- ArrayList<> cannot be resolved to a type
- Simple way to repeat a string
- Unreachable statement compile error in Java
- Reflection generic get field value
- How to use ArrayUtils for array of objects, it doesn’t delete the content of an array
- The ResourceConfig instance does not contain any root resource classes
- Which exception to throw for invalid input which is valid from client perspective
- Efficient way to do batch INSERTS with JDBC
- wrong ELF class: ELFCLASS32
- Invalid signature file digest for Manifest main attributes exception while trying to run jar file
- What is `AnyType` in java generics
- “insufficient memory for the Java Runtime Environment ” message in eclipse
- How to ping an IP address
- Java Not Greater than Or Equal to Operator for Char Type
- The request was rejected because no multipart boundary was found in springboot
- Static Final Long serialVersionUID = 1L
- Which Eclipse IDE version to choose?
- Error: The processing instruction target matching “[xX][mM][lL]” is not allowed
- Unable to resolve host “
” No address associated with host name - Why int[] a = new int[1] instead of just int a?
- getOutputStream() has already been called for this response
- Could not find or load main class with a Jar File
- “uses unchecked or unsafe operations” [duplicate]
- How do I enable index downloads in Eclipse for Maven dependency search?
- How to convert java.util.Date to java.sql.Date?