# Trying to adapt a try-catch block from a Java tutorial

**URL:** <https://community.haxe.org/t/trying-to-adapt-a-try-catch-block-from-a-java-tutorial/356>\
**Category:** Haxe\
**Created:** [January 25, 2018, 3:04am UTC](https://community.haxe.org/t/trying-to-adapt-a-try-catch-block-from-a-java-tutorial/356 "2018-01-25T03:04:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![AaronsConsole](https://community.haxe.org/user_avatar/community.haxe.org/aaronsconsole/32/252_2.png) [@AaronsConsole](https://community.haxe.org/u/AaronsConsole)\
**Post date:** [January 25, 2018, 3:04am UTC](https://community.haxe.org/t/trying-to-adapt-a-try-catch-block-from-a-java-tutorial/356/1 "2018-01-25T03:04:14Z")

</div>

Can’t make sense of the format in the docs:

```
try try-expr
catch(varName1:Type1) catch-expr-1
catch(varName2:Type2) catch-expr-2

```

I’ve seen elsewhere people using a Haxe format similar to Java. I want to make something like this:

//while user is not entering anything usable:  
try {  
// entry variable was via user input on the command line  
var number = Std.parseInt(entry);  
} catch(e:Dynamic) {  
trace(e + “Please type in a number”);  
}

I haven’t even gotten to the loop yet. When compiling, **var number** creates an **unknown identifier** error.

If I declare `var number = 0`, outside of the try block and `number= Std.parseInt(entry);` inside the try part, the code compiles but the user typing in a non-number causes the program to crash (the try block has no effect).

---

<div class="post-metadata">

**Author:** ![elsassph](https://community.haxe.org/user_avatar/community.haxe.org/elsassph/32/60_2.png) [@elsassph](https://community.haxe.org/u/elsassph)\
**Post date:** [January 25, 2018, 10:32am UTC](https://community.haxe.org/t/trying-to-adapt-a-try-catch-block-from-a-java-tutorial/356/2 "2018-01-25T10:32:15Z")

</div>

It’s all normal, Haxe isn’t Java 🙂

- `Std.parseInt` isn’t supposed to throw an error but return `null` for invalid input:  
[Std - Haxe 4.2.1 API](https://api.haxe.org/Std.html#parseInt)
- `var number` has to be declared outside the `try` block because variables are locally scoped

If you want to experience an exception you can try:

```haxe
try {
  var a:Array<String> = null;
  a.push('hello');
} catch (e:Dynamic) {
  trace('oops');
}

```
