我目前正在维护一段代码,它有点“异常快乐”。基本上,任何方法或任何东西都会抛出异常。我将努力解决这一问题,但同时,我想知道在较小的代码块(如方法)中处理单个异常的最佳方法是什么。这样做是不是更好:
public void aMethod()
try {
//Lots of code in here. Many lines.
} catch(Exception e) {
// TODO - Handle All Exceptions, but fairly generically
}
}
或者类似这样的东西:
public void bMethod() {
tr
Exception和IOException都是编译时检查的异常。
但是,我们不能在catch块中使用IOException。但是我们可以在catch块中使用Exception,原因是什么。
import java.io.*;
class Demo{
public static void main(String args[]){
try{
}catch(IOException e){ // Does not compile
}
try{
我正在使用try catch在try块中打印相关消息,或者知道在哪种方法中发生了错误。
代码段
for (const searchUrl of savedSearchUrls) {
console.log("here");
// function will get all the links of profiles in saved search url
try {
const links = await scrapeFromurl(browser, searchUrl);
try {
saveToDataba
我的高级同事告诉我要将每个方法包装在一个try-catch块中,这样他们就可以跟踪异常发生的位置,从而帮助调试问题更快。是否最好将每个方法包装在这样的试捕获中:
Public int foo()
{
try
{
//do something
}catch(Exeception ex)
{
//do something with ex
}
}
还是在我认为可能发生的情况下捕获异常更好?例如,使用数组做某事可能会导致IndexOutOfRangeException的发生。
//wrap this in try catch
int[]
我花了一些时间通过大学时使用的教科书重新学习Java。在他们关于异常的解释的末尾,他们提供了以下代码:
public class ReadingObjects {
public static void main(String[] args) {
try{
FileInputStream fis = new FileInputStream("objects");
ObjectInputStream ois = new ObjectInputStream(fis);
try{
我有你的标准try/catch语句:
try
{
// Do a bunch of stuff
}
catch ( Exception e )
{
throw e;
}
有没有办法确定可以从我的代码中捕获哪些可能的异常,而不试图强制我的代码失败,以便查看e是什么类型的异常?
例如,如果我做了一些HTTP调用,或者一些我想要以不同方式处理的JSON内容,我的代码可能如下所示:
try
{
// Do a bunch of stuff
}
catch ( HttpException e )
{
// Do something
throw e;
}
catch ( JSONExc
处理异常而不必到处放置try/catch块的最佳实践是什么?
我的想法是创建一个专门用于接收和处理异常的类,但我想知道这是不是一个好的设计思想。这样的类将接收异常,然后根据其类型或错误代码决定如何处理它,甚至可以解析堆栈跟踪以获取特定信息,等等。
下面是其背后的基本思想和实现:
public class ExceptionHandler
{
public static void Handle(Exception e)
{
if (e.GetBaseException().GetType() == typeof(ArgumentException))
我想重现一部分InterruptedException行为,但我不明白它是如何工作的……
所以我有这样的代码:
public static void main(String [] args){
try{
}catch(InterruptedException ie){
}
}
当我试图编译它时,我得到了这个编译器错误
Unreachable catch block for InterruptedException. This exception is never thrown from the try statement body
我做了一个自定义异常,这并不是一个真正
我不确定如何在GUI中管理异常;我的目标是让用户知道是否发生了错误,并显示一条易于理解的消息。
我正在考虑做这样的事情:
// I'm inside an actionPerformed() method
try {
// do whatever I have to do here
} catch (KnownBusinessException1 e) {
// inform the user and do something;
// most times simply inform the user that it wasn't possible to
假设我有一个try块,其中包含3条语句,并且所有语句都会导致异常。我希望所有3个异常都由它们相关的catch块来处理。有可能吗?
就像这样-->
class multicatch
{
public static void main(String[] args)
{
int[] c={1};
String s="this is a false integer";
try
{
int x=5/args.length;
c[10]=12;
我正在尝试使用boost date_time检查日期是否有效。文档说,如果日期无效,它将抛出异常。现在,如果日期确实无效,我一直在尝试使用try-catch,但不知何故,我的程序仍然被抛出并停止。
简单的测试代码:
#include "boost/date_time/gregorian/gregorian.hpp"
#include <iostream>
int main()
{
int year = 2013;
int month = 1;
int day = 50;
try
{
boost::grego
我对如何在C#中抛出异常感到非常困惑。如果出现异常,则在try块中,1.它将被抛给catch块,2。如果并且只有当catch块捕获它时,才会执行finally块。3.在catch语句捕获的情况下,最后执行finally块。
但是,当我试图运行下面的程序时,输出是A,B,不是BA.Is,我的理解有什么问题吗?谢谢。
class Program
{
public static void Main(string[] args)
{
try
{
int a = 2;
int b = 10 /