Questions tagged [try-catch]

try-catch is a syntactic construct for catching exceptions raised by a code section

The try-catch construct for exception handling is used by a wide group of languages, like C#, C++, Matlab, Python, Java, and JavaScript. An example of this construct is shown below.

try
{
    Some code
}
catch(exception e)
{
    Handle exception
}

References:

8733 questions
3
votes
3 answers

When to catch the exceptions (high level vs. low level)?

I have created the following classes: Abstract logger class Three subclasses of the abstract logger class which implement actual loggers Class which acts as an interface to the logger The three subclasses can throw exceptions (creating file,…
machinery
  • 5,972
  • 12
  • 67
  • 118
3
votes
4 answers

How to read a txt file into a 2d Array Java

Quick question, how would I go about reading a txt file into a 2d array? I need to but each line into an array in order to later on use it for measuring summation of length and width. This will be used for the game of life. CODE public…
user5129558
3
votes
2 answers

Try with resources Statement for JDBC in java

Useful piece of code for Hive JDBC: Connection con = null; Statement stmt = null try { Class.forName("org.apache.hive.jdbc.HiveDriver"); con = DriverManager.getConnection(connectionUri, userName,…
Dev
  • 13,492
  • 19
  • 81
  • 174
3
votes
2 answers

Erlang Error Handling , X unsafe in 'try'

Can anyone enlighten me as to why this bit of code spits back that the X is unsafe in 'try', well I know why, but more so how to fix it. try X = lists:append(lists:zipwith3(fun(X, Y, Z) -> [X, Y, Z] end, Data1, Data2, Data3)) of MP -> X …
BAR
  • 15,909
  • 27
  • 97
  • 185
3
votes
1 answer

php try catch error reporting

I have an application which relies on a soap server to produce content. Additionally authentication to the site is based on a separate LDAP server. Obviously if either of these are down or not responding the site is down. I am trying to lay out…
Chris
  • 11,780
  • 13
  • 48
  • 70
3
votes
3 answers

In flash AS3 How to put single try catch code, in order to catch any errors in whole class?

In Flash AS3 I wanna write the single try catch block in order to catch any errors in whole class. For example, I have a lot of functions in myClass.as. I don't wanna write in each function try catch blocks in order to catch errors in this…
Almas Adilbek
  • 4,371
  • 10
  • 58
  • 97
3
votes
3 answers

Try/Catch or If/Else?

Hello Guys im having trouble trying to figure out how to make this script to work, im very new on scripting but i do understand most of it but still figuring out some things. try { Test-Connection -Computername $_ -count 1 -ErrorAction Stop }…
3
votes
2 answers

Possible to have multiple Python except statements when using try (without searching for a specific except)?

This is my current code: def get_queryset(self) pk = self.kwargs['pk'] try: postD = PostD.objects.get(pk=pk) # In the line below, PostReply.objects.filter(postD=postD) is # not guaranteed to exist, so I am using a…
SilentDev
  • 20,997
  • 28
  • 111
  • 214
3
votes
1 answer

capture part of value of filename that caused error using basename

I'm on a windows box, and I'm using basename to extract file names from some directories. Apparently, there is a limit to the size that a file name can be, otherwise basename throws an error (earlier I was on a linux and there I don't remember a…
Rorschach
  • 31,301
  • 5
  • 78
  • 129
3
votes
3 answers

Try catch block with an if statement

So I got some problems implementing a try catch block in my program. It's quite simple, all I want is to throw an exception whenever the user enters a 0 or less into my dialog window. Here is my code: try { if (this.sides <= 0); } catch…
Essej
  • 892
  • 6
  • 18
  • 33
3
votes
0 answers

Why can't you catch a type parameter in Java?

Java does not let you catch type parameters. For example, the following (contrived) code causes a compile-time error: private static String callMethod() { try { return SomeUtilityClass.method(); catch(E e) { …
Adam Burley
  • 5,551
  • 4
  • 51
  • 72
3
votes
1 answer

newbie: throw new exception - can we change exception name?

I'm trying to work with exceptions. So I have something like: If something bad occurs: throw new CreateContactException($codigo, $result->msg); Later on, I will, try and if not ok, catch: try { createContact(); } catch(CreateContactException…
MEM
  • 30,529
  • 42
  • 121
  • 191
3
votes
1 answer

Laravel Good Practices with Exceptions and Try&Catch Blocks

I'm building a web app where the customers can purchase many different plans and I'm using Stripe API for the payments. When a customer wants to buy a plan, it has to fill the credit card details and the email too. So, I get all this form data in my…
polinicles
  • 111
  • 1
  • 9
3
votes
1 answer

This time-out error ('TimeOutError') is not being caught

I cannot understand why sometimes I cannot catch the TimeOutError inside my flash_serial_buffer method. When running my program I sometimes get a TimeOutError that is not caught and I cannot understand why. I indicate the code of the signal handlers…
LPS
  • 375
  • 3
  • 16
3
votes
1 answer

Why to use `Try` in for-comprehension?

While dealing with error-handling in Scala, I came to the point where I asked myself, whether Trys in a for-comprehension make sense. Please regard the unit test given below. This test shows two approaches: The first approach (with call) embeds the…
Martin Senne
  • 5,939
  • 6
  • 30
  • 47
1 2 3
99
100