Loading...
Logo
Processing Forum
hi
why is the following code with error;
and if u know can u tell me a solution, plz 
:D
Copy code
  1. import java.util.Map;

  2. Map<Integer,Map<Integer,Boolean>> a = new HashMap();
  3. Map<Integer,Boolean> b = new HashMap();
  4. b.put(1,false);
  5. a.put(5,b);
  6. Map<Integer,Boolean> c = a.get(5);

  7. try {
  8.    boolean d =   c.get(2);
  9.   } catch (IOException e) {
  10.     e.printStackTrace();
  11.     boolean d =   c.get(1);
  12.   }
whys is catch giving error?
im i using catch for wht he is not made to do.
any way my goal why try and catch, is that: if anything on try fail(error), the program will run catch
ty
:D

Replies(4)


whys is catch giving error?
Might be useful if you posted the error message.

Note:
  • Line 11 will throw a NullPointerException error if c == null but line 12 will only catch IOExceptions.
  • The HashMap get method does not throw exceptions - it returns null if the key cannot be found.

Try the following


Copy code
  1. import java.util.Map;
  2. void setup(){
  3.   Map<Integer,Map<Integer,Boolean>> a = new HashMap();
  4.   Map<Integer,Boolean> b = new HashMap();
  5.   b.put(1,false);
  6.   a.put(5,b);
  7.   Map<Integer,Boolean> c = a.get(5);

  8.   try {
  9.      boolean d =   c.get(2);
  10.   } catch (Exception e) {  // catch ALL exceptions thrown since try
  11.       e.printStackTrace();
  12.       boolean d =   c.get(1);  
  13.   }
  14. }

Note that an exception thrown in the catch section is not detected with the same try

thank UUUUUUU soooo MUTCH!!!!!!!!!!!!!!!!!!!!!

:D
Some remarks:
- quarks is right...
- Don't use try / catch to handle a NullPointerException! Just use: if (c == null) // Handle the case
- Anyway, if c is null, the code in catch will throw the same exception...
- Your d boolean is useless, since its scope is limited to the { } enclosing it: in other words, you cannot use it after the try / catch.
- The READ PLZ and READ NOW your put in your subjects are:
  o Useless
  o Annoying
  o And thus, seen as a breach of etiquette of the forum (or any other).
So, avoid them, please. Thanks.
Just some remark:

line 12 in original code:
Copy code
  1.   } catch (IOException e) {
would even prevent the code from compiling. Because  in the below try/catch block, IOException isn't even getting thrown. At best c (which is HashMap) can throw some fatal or NullPointerException, but not IOException (which is only thrown by what's called in java IO (Input Output) operations: reding /writing to files, reading/writing from internet and so on. 
Copy code
  1.   try {
  2.      boolean d =   c.get(2);
  3.   } catch (IOException e) {  // catch ALL exceptions thrown since try

You should also differentiate situations when: 
an exception is thrown when your sketch is actually running, or when you just get compilation error (this just means that you're not using correct syntax or not following proper semantics in your code (eg. catching exception which is never thrown).