8 Replies - 10577 Views - Last Post: 17 March 2009 - 02:31 PM Rate Topic: -----

#1 conartist6   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 17-March 09

Exception not being try/caught

Posted 17 March 2009 - 01:42 PM

My problem is that I am trying to match a regex statement in a Scanner findInLine() function. When it works, it works, but when there is an invalid string it throws a java.lang.InvalidStateException that I cannot seem to catch. If I am not meant to be able to catch this exception, then how am I supposed to avoid my program crashing on an invalid search? here are the relevant bits of my code.

String treeString = "(6 (5 (1) (2)) (3))";	
Scanner sc = new Scanner(treeString);
try{
	 sc.findInLine("(\\() (\\d+) ");
}
catch (java.lang.IllegalStateException e) {System.out.println("caught");}
catch (Throwable e) {System.out.println("caught");



Neither of those catch statements work singly, together, or at all. My program always exits on a stack trace of the InvalidStateException. help?

Is This A Good Question/Topic? 0
  • +

Replies To: Exception not being try/caught

#2 Locke   User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 552
  • View blog
  • Posts: 5,624
  • Joined: 20-March 08

Re: Exception not being try/caught

Posted 17 March 2009 - 01:44 PM

You never caught an InvalidStateException. You caught an IllegalStateException. :D

This post has been edited by Locke: 17 March 2009 - 01:46 PM

Was This Post Helpful? 0
  • +
  • -

#3 conartist6   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 17-March 09

Re: Exception not being try/caught

Posted 17 March 2009 - 01:49 PM

View PostLocke, on 17 Mar, 2009 - 12:44 PM, said:

You never caught an InvalidStateException. You caught an IllegalStateException. :D


But I never got an InvalidStateException! maybe this is the problem... and anyway, catching anything throwable should catch any exception at all...

edit: I just noticed that eclipse is giving me the following error on that line: java.lang.InvalidStateException cannot be resolved to a type. But I still haven't asked for an InvalidStateException have I??

edit edit: findInLine claims that it throws that exception if its Scanner is closed. So for some reason not finding a match in the line is closing the scanner and throwing an uncatchable exception. What java god have I annoyed?

This post has been edited by conartist6: 17 March 2009 - 01:58 PM

Was This Post Helpful? 0
  • +
  • -

#4 Locke   User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 552
  • View blog
  • Posts: 5,624
  • Joined: 20-March 08

Re: Exception not being try/caught

Posted 17 March 2009 - 01:54 PM

I dunno...I've never tried to catch a Throwable, so I don't exactly know if that will work.

You could just have 1 catch block and catch a generic Exception.

catch (Exception e)
{

}


That will catch any exception.
Was This Post Helpful? 0
  • +
  • -

#5 conartist6   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 17-March 09

Re: Exception not being try/caught

Posted 17 March 2009 - 02:00 PM

I tried catching an Exception. I believe Exception is a subclass of Throwable so Throwable will also catch any exception. Plus I tried catch Exception before I tried catch Throwable and just tried it again and still neither of those work.
Was This Post Helpful? 0
  • +
  • -

#6 Locke   User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 552
  • View blog
  • Posts: 5,624
  • Joined: 20-March 08

Re: Exception not being try/caught

Posted 17 March 2009 - 02:09 PM

Upon further research...There's no such thing as an InvalidStateException. (And yes, you're right about Exception being a subclass of Throwable...)

Hm...I don't know why it's closing the scanner stream. The only thing I can think of is that you're not using that method for anything. The findInLine() method returns a string.
Was This Post Helpful? 0
  • +
  • -

#7 conartist6   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 17-March 09

Re: Exception not being try/caught

Posted 17 March 2009 - 02:13 PM

I found something. If I change that line to
try{
	sc.next("(\\()(\\d+) ");
}
catch (IllegalStateException e) {System.out.println("caught");}
catch (Throwable e) {System.out.println("caught");}


then I get a caught from my catch statement before terminates on an IllegalStateException

:/

edit: That catch was from the Throwable clause, not the Illegal* one.
commenting the throwable clause gets me a generic InputMismatchException.

This post has been edited by conartist6: 17 March 2009 - 02:16 PM

Was This Post Helpful? 0
  • +
  • -

#8 Locke   User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 552
  • View blog
  • Posts: 5,624
  • Joined: 20-March 08

Re: Exception not being try/caught

Posted 17 March 2009 - 02:16 PM

You have to be getting an IllegalStateException after the fact. Are you ever trying to use that Scanner object after that snippet of code? Because that would make sense I suppose if that snippet closed the stream (for some unknown reason).

This post has been edited by Locke: 17 March 2009 - 02:17 PM

Was This Post Helpful? 0
  • +
  • -

#9 conartist6   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 17-March 09

Re: Exception not being try/caught

Posted 17 March 2009 - 02:31 PM

I was about to add that. So the first block of code now works correctly catching an InputMismatchException from the next statement. NOW I am having the problem you just described. My code looks like this now.

Scanner sc = new Scanner(treeString);
try{
	 sc.findInLine("(\\() (\\d+) ");
}
catch (InputMismatchExcpetion e) {System.out.println("caught");}
MatchResult result = sc.match();


I'm about to double-check that MatchResult is really what I should still be using, and I don't think it is. The code was cannibalized from Java's doc page on scanners anyway. Wish they would've documented it a little better.

edit: done. result = sc.findInLine. That should be all. Except now I'm having problems with regular expressions. New topic though.

This post has been edited by conartist6: 17 March 2009 - 03:04 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1