0

I am using the Arity Arithmetic Engine for my calculations. So I wanted to know what are all the exceptions that can arise when I use

double res = Symbols.eval("string");

Im enclosing this in a try block as

try{
double res = Symbols.eval("Expression");
return res;
}
catch(SyntaxException s)
{
System.out.println("Exception..!");
}

So i want to know what all exceptions are generated So that I can know where I may be going wrong.

Aftab Khan
  • 3,853
  • 1
  • 21
  • 30
  • Please consider accepting an answer below, as they all provide useful information regarding your question. This will make it easier for others with similar questions to find the answer. – Akshay Jul 18 '14 at 18:31
  • No it doesnt completely answer my question I've posted further comments on each post – Aftab Khan Jul 18 '14 at 18:45

3 Answers3

1

Not sure I fully understand the question, but if you catch any exception and then print the stack trace you can more accurately figure out what type of exception was thrown and what caused it. This will allow you to accurately distinguish SyntaxErrors which you mentioned from any other exception which could arise for other reasons.

try
{
    double res = Symbols.eval("Expression");
    return res;
}
catch(Exception e)
{
    e.printStackTrace();
}

Printing the stack trace is very important as it will allow you to figure out exactly where in your code the issue is coming from. If you just do a simple System.out.println("Error happned") you aren't going to get as much information.

When you print the stack trace you can see the precise chain of method calls that led to the ultimate failure down to the line, which makes it easy to find out why the error is arising.

Akshay
  • 814
  • 6
  • 19
  • Symbols.eval is contained in a jar file "arity.jar". So im not sure it will take me to the code which caused the error – Aftab Khan Jul 18 '14 at 18:39
  • @AftabKhan Actually printing the stack trace will show you exactly where the error occurred regardless of whether the method is in a jar or not. It's one of the most useful ways to debug. – Akshay Jul 18 '14 at 18:57
  • Code is getting compiled. But I dont know what the code is as it is imported from a Jar file. – Aftab Khan Jul 18 '14 at 18:58
  • Sorry I didn't understand your comment. See my new one above. – Akshay Jul 18 '14 at 18:58
  • Code is getting compiled. But I dont know what the code is as it is imported from a Jar file. I have attached it as a library to my project from which I am executing the API. – Aftab Khan Jul 18 '14 at 19:00
  • Yes, that's fine. Printing the stack trace will still tell you exactly what Exception was thrown, where it was thrown, and why it was thrown. – Akshay Jul 18 '14 at 19:01
  • In fact, you can also look at the API's documentation to figure out what are exceptions that might reasonably be thrown and how to avoid them. – Akshay Jul 18 '14 at 19:03
  • Problem is Its Javadoc is not accessible. – Aftab Khan Jul 18 '14 at 19:04
  • Oh, that's still ok. The beauty of printStackTrace is that it will tell you what exception was thrown. – Akshay Jul 18 '14 at 19:07
  • Thanks for the info. Will check that and get back. – Aftab Khan Jul 18 '14 at 19:09
0

I am unfamiliar with Arity, especially since all the link I find leads to 404 error pages. But aside that, you can catch known exceptions, and a final catch for any other exception that you can refactor later on to handle it.

try {
    double res = Symbols.eval("Expression");
    return res;
} catch (SyntaxException s) {
    System.err.println("Syntax exception");
} catch (Exception e) {
    System.err.println("Unknown exception caught!");
}
Yanick Rochon
  • 51,409
  • 25
  • 133
  • 214
  • SyntaxException as far as i know is not only to throw "syntax exception", it describes the error encountered while evaluating – Aftab Khan Jul 18 '14 at 18:38
  • [link](http://www.java2s.com/Code/Jar/a/Downloadarity217jar.htm) [link](http://www.developerfusion.com/project/62854/arity/) These links will give you description of what I am trying to ask. PS: Its javadoc is unreachable – Aftab Khan Jul 18 '14 at 18:43
  • 1
    Yeah, I have seen these two pages... probably the only pages where you can read anything about Arity, as a matter of fact! In any case, the `SyntaxException` seems to be the only error being thrown from `Symbols.eval` anyhow. – Yanick Rochon Jul 18 '14 at 19:28
0

I'm sure System.out.println(e.getMessage()); will give you more usable information.

alex
  • 8,904
  • 6
  • 49
  • 75
  • This seems a bit more generic one. And since Symbols is contained in a third party jar file, I was looking for the error it generates possibly thrown by SyntaxException.java class – Aftab Khan Jul 18 '14 at 18:41