About Question enthuware.ocpjp.i.v11.2.826 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
dimitrilc
Posts: 35
Joined: Sat Jun 06, 2020 4:51 pm
Contact:

About Question enthuware.ocpjp.i.v11.2.826 :

Post by dimitrilc »

So MyException is unable to catch any exception except ItSelf ?

What if you want MyException to be able to catch ArrayIndexOutOfBoundsException, what would the code inside class MyException look like? Or is this outside of the scope for the test?

Or, alternatively, to be able to catch ArrayIndexOutOfBoundsException, is this how we would do it with the limited knowledge that we have for this test?

Code: Select all

        try{
            doTest();
        }
        catch(MyException me){
            System.out.println(me);
        } catch (ArrayIndexOutOfBoundsException e){ }
In summary, I want to make the conclusion that a custom exception that extends Exception/Throwable is pretty much useless for catching any other type of error/exception. It can only catch itself.

Although I will not conclude this hypothesis for now, I think custom code can be added to MyException for it to be able to catch other exceptions/errors, but I don't know if that is outside of the scope of the test or not.

admin
Site Admin
Posts: 10062
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.i.v11.2.826 :

Post by admin »

Well, you can catch any subclass of MyException as well. It is no different from catching java.io.IOException. If you have catch(IOException e), it will catch subclasses such as FileNotFoundExcception and EOFException.

Yes, that is how you should catch MyException and AIOOBE. Or you could catch a common superclass such as catch(Exception ), it will catch both of them.

But more importantly your question indicates a fundamental issue with your understanding of exceptions. Why would you want to catch AIOOBE "using" MyException (whatever that means)? You might want to go through a good book and read up on it before attempting questions.
If you like our products and services, please help us by posting your review here.

dimitrilc
Posts: 35
Joined: Sat Jun 06, 2020 4:51 pm
Contact:

Re: About Question enthuware.ocpjp.i.v11.2.826 :

Post by dimitrilc »

I originally thought that exceptions are classes with code that can handle exceptions, so a class that extends Exception should also inherit child methods(which are supposedly algos/mechanisms to detect exceptions), but I guess I was wrong.

When I looked at the source code, pretty much all the <? extends Throwable> classes do are calling the super constructors. So when you are creating a custom exception:
1. You are only creating a new detection mechanism for business or logical exceptions, not new [machine level]exception detection mechanisms.
2. You want to override the default exceptions.

There was also the concept of inheritance that I forgot to apply when reading exceptions.
Cat < Mammal < Animal
Lizard < Animal
Cat is not a Lizard

AIOOB < RuntimeException < Exception
MyException < Exception
AIOOB is not a MyException

From the information that I have gathered so far, Java exception classes are just high level abstraction sugar that translate real exceptions into human-readable form. The HotSpot JVM is maybe using c++ features to catch the exceptions, c++ is maybe using something else, and the chain goes on until you reach machine code. An exception is thrown if the JVM returns 'athrow', I guess. https://www.infoworld.com/article/20768 ... tions.html

Hypothetically, the chain of error throwing is maybe be something like this:

Code: Select all

CPU > exception or machine language? > jvm > athrow
or like this:

Code: Select all

Jvm checks for exception before passing instruction to CPU using internal c++ code > athrow
or like this:

Code: Select all

Jvm uses Operating System features for check/handle exceptions > athrow
Obviously, if an exception is not caught by either the JVM or the CPU's detection mechanism, then the CPU might hang.
https://softwareengineering.stackexchan ... de-by-zero

I guess a lot of CPU exploits look for illegal instructions that are unchecked by either the OS or the CPU itself? I'm no hacker, but after reading through this low level exception stuff, I was able to deduce some assumptions about this topic.

The above scenarios are only hypothetical and understanding this process at a low level is probably out of scope for the exam. So I don't want to dive too deep into this topic. The important thing here is to understand what the exception classes do from a 'Java' level, not how this process works under the hood.

I was confused between multiple concepts, what exception classes source codes really do, logical/business exceptions vs machine-level exceptions, and inheritance.

I think I understand Java exceptions very well now. Thanks.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 101 guests